blob: de2c1bc90aa8f4957333fd0f5f0d36b73c05dba2 [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
Dan Gohman94d7a5f2008-06-21 19:18:17 +0000100 // During scheduling, the NodeId field of SDNode is used to map SDNodes
101 // to their associated SUnits by holding SUnits table indices. A value
102 // of -1 means the SDNode does not yet have an associated SUnit.
103 for (SelectionDAG::allnodes_iterator NI = DAG.allnodes_begin(),
104 E = DAG.allnodes_end(); NI != E; ++NI)
105 NI->setNodeId(-1);
106
Evan Chenge165a782006-05-11 23:55:42 +0000107 for (SelectionDAG::allnodes_iterator NI = DAG.allnodes_begin(),
108 E = DAG.allnodes_end(); NI != E; ++NI) {
109 if (isPassiveNode(NI)) // Leaf node, e.g. a TargetImmediate.
110 continue;
111
112 // If this node has already been processed, stop now.
Dan Gohman94d7a5f2008-06-21 19:18:17 +0000113 if (NI->getNodeId() != -1) continue;
Evan Chenge165a782006-05-11 23:55:42 +0000114
115 SUnit *NodeSUnit = NewSUnit(NI);
116
117 // See if anything is flagged to this node, if so, add them to flagged
118 // nodes. Nodes can have at most one flag input and one flag output. Flags
119 // are required the be the last operand and result of a node.
120
121 // Scan up, adding flagged preds to FlaggedNodes.
122 SDNode *N = NI;
Evan Cheng3b97acd2006-08-07 22:12:12 +0000123 if (N->getNumOperands() &&
124 N->getOperand(N->getNumOperands()-1).getValueType() == MVT::Flag) {
125 do {
126 N = N->getOperand(N->getNumOperands()-1).Val;
127 NodeSUnit->FlaggedNodes.push_back(N);
Dan Gohman94d7a5f2008-06-21 19:18:17 +0000128 assert(N->getNodeId() == -1 && "Node already inserted!");
129 N->setNodeId(NodeSUnit->NodeNum);
Evan Cheng3b97acd2006-08-07 22:12:12 +0000130 } while (N->getNumOperands() &&
131 N->getOperand(N->getNumOperands()-1).getValueType()== MVT::Flag);
132 std::reverse(NodeSUnit->FlaggedNodes.begin(),
133 NodeSUnit->FlaggedNodes.end());
Evan Chenge165a782006-05-11 23:55:42 +0000134 }
135
136 // Scan down, adding this node and any flagged succs to FlaggedNodes if they
137 // have a user of the flag operand.
138 N = NI;
139 while (N->getValueType(N->getNumValues()-1) == MVT::Flag) {
140 SDOperand FlagVal(N, N->getNumValues()-1);
141
142 // There are either zero or one users of the Flag result.
143 bool HasFlagUse = false;
144 for (SDNode::use_iterator UI = N->use_begin(), E = N->use_end();
145 UI != E; ++UI)
Roman Levensteindc1adac2008-04-07 10:06:32 +0000146 if (FlagVal.isOperandOf(UI->getUser())) {
Evan Chenge165a782006-05-11 23:55:42 +0000147 HasFlagUse = true;
148 NodeSUnit->FlaggedNodes.push_back(N);
Dan Gohman94d7a5f2008-06-21 19:18:17 +0000149 assert(N->getNodeId() == -1 && "Node already inserted!");
150 N->setNodeId(NodeSUnit->NodeNum);
Roman Levensteindc1adac2008-04-07 10:06:32 +0000151 N = UI->getUser();
Evan Chenge165a782006-05-11 23:55:42 +0000152 break;
153 }
Chris Lattner228a18e2006-08-17 00:09:56 +0000154 if (!HasFlagUse) break;
Evan Chenge165a782006-05-11 23:55:42 +0000155 }
156
157 // Now all flagged nodes are in FlaggedNodes and N is the bottom-most node.
158 // Update the SUnit
159 NodeSUnit->Node = N;
Dan Gohman94d7a5f2008-06-21 19:18:17 +0000160 assert(N->getNodeId() == -1 && "Node already inserted!");
161 N->setNodeId(NodeSUnit->NodeNum);
Evan Chengf10c9732007-10-05 01:39:18 +0000162
163 ComputeLatency(NodeSUnit);
Evan Chenge165a782006-05-11 23:55:42 +0000164 }
165
166 // Pass 2: add the preds, succs, etc.
167 for (unsigned su = 0, e = SUnits.size(); su != e; ++su) {
168 SUnit *SU = &SUnits[su];
169 SDNode *MainNode = SU->Node;
170
171 if (MainNode->isTargetOpcode()) {
172 unsigned Opc = MainNode->getTargetOpcode();
Chris Lattner749c6f62008-01-07 07:27:27 +0000173 const TargetInstrDesc &TID = TII->get(Opc);
Chris Lattner349c4952008-01-07 03:13:06 +0000174 for (unsigned i = 0; i != TID.getNumOperands(); ++i) {
Evan Chenga6fb1b62007-09-25 01:54:36 +0000175 if (TID.getOperandConstraint(i, TOI::TIED_TO) != -1) {
Evan Cheng95f6ede2006-11-04 09:44:31 +0000176 SU->isTwoAddress = true;
177 break;
178 }
179 }
Chris Lattner0ff23962008-01-07 06:42:05 +0000180 if (TID.isCommutable())
Evan Cheng13d41b92006-05-12 01:58:24 +0000181 SU->isCommutable = true;
Evan Chenge165a782006-05-11 23:55:42 +0000182 }
183
184 // Find all predecessors and successors of the group.
185 // Temporarily add N to make code simpler.
186 SU->FlaggedNodes.push_back(MainNode);
187
188 for (unsigned n = 0, e = SU->FlaggedNodes.size(); n != e; ++n) {
189 SDNode *N = SU->FlaggedNodes[n];
Evan Cheng22a52992007-09-28 22:32:30 +0000190 if (N->isTargetOpcode() &&
Chris Lattner349c4952008-01-07 03:13:06 +0000191 TII->get(N->getTargetOpcode()).getImplicitDefs() &&
192 CountResults(N) > TII->get(N->getTargetOpcode()).getNumDefs())
Evan Cheng22a52992007-09-28 22:32:30 +0000193 SU->hasPhysRegDefs = true;
Evan Chenge165a782006-05-11 23:55:42 +0000194
195 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
196 SDNode *OpN = N->getOperand(i).Val;
197 if (isPassiveNode(OpN)) continue; // Not scheduled.
Dan Gohman94d7a5f2008-06-21 19:18:17 +0000198 SUnit *OpSU = &SUnits[OpN->getNodeId()];
Evan Chenge165a782006-05-11 23:55:42 +0000199 assert(OpSU && "Node has no SUnit!");
200 if (OpSU == SU) continue; // In the same group.
201
Duncan Sands83ec4b62008-06-06 12:08:01 +0000202 MVT OpVT = N->getOperand(i).getValueType();
Evan Chenge165a782006-05-11 23:55:42 +0000203 assert(OpVT != MVT::Flag && "Flagged nodes should be in same sunit!");
204 bool isChain = OpVT == MVT::Other;
Evan Chenga6fb1b62007-09-25 01:54:36 +0000205
206 unsigned PhysReg = 0;
207 int Cost = 1;
208 // Determine if this is a physical register dependency.
Dan Gohman6f0d0242008-02-10 18:45:23 +0000209 CheckForPhysRegDependency(OpN, N, i, TRI, TII, PhysReg, Cost);
Evan Chenga6fb1b62007-09-25 01:54:36 +0000210 SU->addPred(OpSU, isChain, false, PhysReg, Cost);
Evan Chenge165a782006-05-11 23:55:42 +0000211 }
212 }
213
214 // Remove MainNode from FlaggedNodes again.
215 SU->FlaggedNodes.pop_back();
216 }
Evan Chenge165a782006-05-11 23:55:42 +0000217}
218
Evan Chengf10c9732007-10-05 01:39:18 +0000219void ScheduleDAG::ComputeLatency(SUnit *SU) {
220 const InstrItineraryData &InstrItins = TM.getInstrItineraryData();
221
222 // Compute the latency for the node. We use the sum of the latencies for
223 // all nodes flagged together into this SUnit.
224 if (InstrItins.isEmpty()) {
225 // No latency information.
226 SU->Latency = 1;
227 } else {
228 SU->Latency = 0;
229 if (SU->Node->isTargetOpcode()) {
Chris Lattnerba6da5d2008-01-07 02:46:03 +0000230 unsigned SchedClass =
231 TII->get(SU->Node->getTargetOpcode()).getSchedClass();
Dan Gohmancfbb2f02008-03-25 21:45:14 +0000232 const InstrStage *S = InstrItins.begin(SchedClass);
233 const InstrStage *E = InstrItins.end(SchedClass);
Evan Chengf10c9732007-10-05 01:39:18 +0000234 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()) {
Chris Lattnerba6da5d2008-01-07 02:46:03 +0000240 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 }
246 }
247 }
248}
249
Roman Levensteind86449e2008-03-04 11:19:43 +0000250/// CalculateDepths - compute depths using algorithms for the longest
251/// paths in the DAG
Evan Chenge165a782006-05-11 23:55:42 +0000252void ScheduleDAG::CalculateDepths() {
Roman Levensteind86449e2008-03-04 11:19:43 +0000253 unsigned DAGSize = SUnits.size();
254 std::vector<unsigned> InDegree(DAGSize);
255 std::vector<SUnit*> WorkList;
256 WorkList.reserve(DAGSize);
Evan Chenge165a782006-05-11 23:55:42 +0000257
Roman Levensteind86449e2008-03-04 11:19:43 +0000258 // Initialize the data structures
259 for (unsigned i = 0, e = DAGSize; i != e; ++i) {
260 SUnit *SU = &SUnits[i];
261 int NodeNum = SU->NodeNum;
262 unsigned Degree = SU->Preds.size();
263 InDegree[NodeNum] = Degree;
264 SU->Depth = 0;
265
266 // Is it a node without dependencies?
267 if (Degree == 0) {
268 assert(SU->Preds.empty() && "SUnit should have no predecessors");
269 // Collect leaf nodes
270 WorkList.push_back(SU);
271 }
272 }
273
274 // Process nodes in the topological order
Evan Cheng99126282007-07-06 01:37:28 +0000275 while (!WorkList.empty()) {
Roman Levensteind86449e2008-03-04 11:19:43 +0000276 SUnit *SU = WorkList.back();
Evan Cheng99126282007-07-06 01:37:28 +0000277 WorkList.pop_back();
Roman Levensteind86449e2008-03-04 11:19:43 +0000278 unsigned &SUDepth = SU->Depth;
279
280 // Use dynamic programming:
281 // When current node is being processed, all of its dependencies
282 // are already processed.
283 // So, just iterate over all predecessors and take the longest path
284 for (SUnit::const_pred_iterator I = SU->Preds.begin(), E = SU->Preds.end();
285 I != E; ++I) {
286 unsigned PredDepth = I->Dep->Depth;
287 if (PredDepth+1 > SUDepth) {
288 SUDepth = PredDepth + 1;
289 }
290 }
291
292 // Update InDegrees of all nodes depending on current SUnit
293 for (SUnit::const_succ_iterator I = SU->Succs.begin(), E = SU->Succs.end();
294 I != E; ++I) {
295 SUnit *SU = I->Dep;
296 if (!--InDegree[SU->NodeNum])
297 // If all dependencies of the node are processed already,
298 // then the longest path for the node can be computed now
299 WorkList.push_back(SU);
Evan Cheng99126282007-07-06 01:37:28 +0000300 }
Evan Cheng626da3d2006-05-12 06:05:18 +0000301 }
Evan Chenge165a782006-05-11 23:55:42 +0000302}
Evan Cheng99126282007-07-06 01:37:28 +0000303
Roman Levensteind86449e2008-03-04 11:19:43 +0000304/// CalculateHeights - compute heights using algorithms for the longest
305/// paths in the DAG
Evan Chenge165a782006-05-11 23:55:42 +0000306void ScheduleDAG::CalculateHeights() {
Roman Levensteind86449e2008-03-04 11:19:43 +0000307 unsigned DAGSize = SUnits.size();
308 std::vector<unsigned> InDegree(DAGSize);
309 std::vector<SUnit*> WorkList;
310 WorkList.reserve(DAGSize);
Evan Cheng99126282007-07-06 01:37:28 +0000311
Roman Levensteind86449e2008-03-04 11:19:43 +0000312 // Initialize the data structures
313 for (unsigned i = 0, e = DAGSize; i != e; ++i) {
314 SUnit *SU = &SUnits[i];
315 int NodeNum = SU->NodeNum;
316 unsigned Degree = SU->Succs.size();
317 InDegree[NodeNum] = Degree;
318 SU->Height = 0;
319
320 // Is it a node without dependencies?
321 if (Degree == 0) {
322 assert(SU->Succs.empty() && "Something wrong");
323 assert(WorkList.empty() && "Should be empty");
324 // Collect leaf nodes
325 WorkList.push_back(SU);
326 }
327 }
328
329 // Process nodes in the topological order
Evan Cheng99126282007-07-06 01:37:28 +0000330 while (!WorkList.empty()) {
Roman Levensteind86449e2008-03-04 11:19:43 +0000331 SUnit *SU = WorkList.back();
Evan Cheng99126282007-07-06 01:37:28 +0000332 WorkList.pop_back();
Roman Levensteind86449e2008-03-04 11:19:43 +0000333 unsigned &SUHeight = SU->Height;
334
335 // Use dynamic programming:
336 // When current node is being processed, all of its dependencies
337 // are already processed.
338 // So, just iterate over all successors and take the longest path
339 for (SUnit::const_succ_iterator I = SU->Succs.begin(), E = SU->Succs.end();
340 I != E; ++I) {
341 unsigned SuccHeight = I->Dep->Height;
342 if (SuccHeight+1 > SUHeight) {
343 SUHeight = SuccHeight + 1;
344 }
345 }
346
347 // Update InDegrees of all nodes depending on current SUnit
348 for (SUnit::const_pred_iterator I = SU->Preds.begin(), E = SU->Preds.end();
349 I != E; ++I) {
350 SUnit *SU = I->Dep;
351 if (!--InDegree[SU->NodeNum])
352 // If all dependencies of the node are processed already,
353 // then the longest path for the node can be computed now
354 WorkList.push_back(SU);
Evan Cheng99126282007-07-06 01:37:28 +0000355 }
356 }
Evan Chenge165a782006-05-11 23:55:42 +0000357}
358
Jim Laskeye6b90fb2005-09-26 21:57:04 +0000359/// CountResults - The results of target nodes have register or immediate
360/// operands first, then an optional chain, and optional flag operands (which do
Dan Gohman027ee7e2008-02-11 19:00:03 +0000361/// not go into the resulting MachineInstr).
Evan Cheng95f6ede2006-11-04 09:44:31 +0000362unsigned ScheduleDAG::CountResults(SDNode *Node) {
Jim Laskeyb6d4c2c2005-09-30 19:15:27 +0000363 unsigned N = Node->getNumValues();
364 while (N && Node->getValueType(N - 1) == MVT::Flag)
Jim Laskeye6b90fb2005-09-26 21:57:04 +0000365 --N;
Jim Laskeyb6d4c2c2005-09-30 19:15:27 +0000366 if (N && Node->getValueType(N - 1) == MVT::Other)
Jim Laskeye6b90fb2005-09-26 21:57:04 +0000367 --N; // Skip over chain result.
368 return N;
369}
370
Dan Gohman69de1932008-02-06 22:27:42 +0000371/// CountOperands - The inputs to target nodes have any actual inputs first,
Dan Gohman42a77882008-02-16 00:36:48 +0000372/// followed by special operands that describe memory references, then an
373/// optional chain operand, then flag operands. Compute the number of
374/// actual operands that will go into the resulting MachineInstr.
Evan Cheng95f6ede2006-11-04 09:44:31 +0000375unsigned ScheduleDAG::CountOperands(SDNode *Node) {
Dan Gohman42a77882008-02-16 00:36:48 +0000376 unsigned N = ComputeMemOperandsEnd(Node);
Dan Gohmancc20cd52008-02-11 19:00:34 +0000377 while (N && isa<MemOperandSDNode>(Node->getOperand(N - 1).Val))
Dan Gohman36b5c132008-04-07 19:35:22 +0000378 --N; // Ignore MEMOPERAND nodes
Dan Gohman69de1932008-02-06 22:27:42 +0000379 return N;
380}
381
Dan Gohman42a77882008-02-16 00:36:48 +0000382/// ComputeMemOperandsEnd - Find the index one past the last MemOperandSDNode
383/// operand
384unsigned ScheduleDAG::ComputeMemOperandsEnd(SDNode *Node) {
Dan Gohman69de1932008-02-06 22:27:42 +0000385 unsigned N = Node->getNumOperands();
386 while (N && Node->getOperand(N - 1).getValueType() == MVT::Flag)
387 --N;
388 if (N && Node->getOperand(N - 1).getValueType() == MVT::Other)
389 --N; // Ignore chain if it exists.
Jim Laskeye6b90fb2005-09-26 21:57:04 +0000390 return N;
391}
392
Jim Laskey60f09922006-07-21 20:57:35 +0000393static const TargetRegisterClass *getInstrOperandRegClass(
Dan Gohman6f0d0242008-02-10 18:45:23 +0000394 const TargetRegisterInfo *TRI,
Jim Laskey60f09922006-07-21 20:57:35 +0000395 const TargetInstrInfo *TII,
Chris Lattner749c6f62008-01-07 07:27:27 +0000396 const TargetInstrDesc &II,
Jim Laskey60f09922006-07-21 20:57:35 +0000397 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 Chenga6fb1b62007-09-25 01:54:36 +0000407void ScheduleDAG::EmitCopyFromReg(SDNode *Node, unsigned ResNo,
Dan Gohman4c8c8302008-06-21 15:52:51 +0000408 bool IsClone, unsigned SrcReg,
Roman Levenstein9cac5252008-04-16 16:15:27 +0000409 DenseMap<SDOperand, unsigned> &VRBaseMap) {
Evan Cheng84097472007-08-02 00:28:15 +0000410 unsigned VRBase = 0;
Dan Gohman6f0d0242008-02-10 18:45:23 +0000411 if (TargetRegisterInfo::isVirtualRegister(SrcReg)) {
Evan Cheng84097472007-08-02 00:28:15 +0000412 // Just use the input register directly!
Dan Gohman4c8c8302008-06-21 15:52:51 +0000413 if (IsClone)
Evan Chenga6fb1b62007-09-25 01:54:36 +0000414 VRBaseMap.erase(SDOperand(Node, ResNo));
Evan Cheng84097472007-08-02 00:28:15 +0000415 bool isNew = VRBaseMap.insert(std::make_pair(SDOperand(Node,ResNo),SrcReg));
Evan Cheng97e60d92008-05-14 21:08:07 +0000416 isNew = isNew; // Silence compiler warning.
Evan Cheng84097472007-08-02 00:28:15 +0000417 assert(isNew && "Node emitted out of order - early");
418 return;
419 }
420
421 // If the node is only used by a CopyToReg and the dest reg is a vreg, use
422 // the CopyToReg'd destination register instead of creating a new vreg.
Evan Chenga6fb1b62007-09-25 01:54:36 +0000423 bool MatchReg = true;
Evan Cheng84097472007-08-02 00:28:15 +0000424 for (SDNode::use_iterator UI = Node->use_begin(), E = Node->use_end();
425 UI != E; ++UI) {
Roman Levensteindc1adac2008-04-07 10:06:32 +0000426 SDNode *Use = UI->getUser();
Evan Chenga6fb1b62007-09-25 01:54:36 +0000427 bool Match = true;
Evan Cheng84097472007-08-02 00:28:15 +0000428 if (Use->getOpcode() == ISD::CopyToReg &&
429 Use->getOperand(2).Val == Node &&
430 Use->getOperand(2).ResNo == ResNo) {
431 unsigned DestReg = cast<RegisterSDNode>(Use->getOperand(1))->getReg();
Dan Gohman6f0d0242008-02-10 18:45:23 +0000432 if (TargetRegisterInfo::isVirtualRegister(DestReg)) {
Evan Cheng84097472007-08-02 00:28:15 +0000433 VRBase = DestReg;
Evan Chenga6fb1b62007-09-25 01:54:36 +0000434 Match = false;
435 } else if (DestReg != SrcReg)
436 Match = false;
437 } else {
438 for (unsigned i = 0, e = Use->getNumOperands(); i != e; ++i) {
439 SDOperand Op = Use->getOperand(i);
Evan Cheng7c07aeb2007-12-14 08:25:15 +0000440 if (Op.Val != Node || Op.ResNo != ResNo)
Evan Chenga6fb1b62007-09-25 01:54:36 +0000441 continue;
Duncan Sands83ec4b62008-06-06 12:08:01 +0000442 MVT VT = Node->getValueType(Op.ResNo);
Evan Chenga6fb1b62007-09-25 01:54:36 +0000443 if (VT != MVT::Other && VT != MVT::Flag)
444 Match = false;
Evan Cheng84097472007-08-02 00:28:15 +0000445 }
446 }
Evan Chenga6fb1b62007-09-25 01:54:36 +0000447 MatchReg &= Match;
448 if (VRBase)
449 break;
Evan Cheng84097472007-08-02 00:28:15 +0000450 }
451
Chris Lattner02b6d252008-03-09 08:49:15 +0000452 const TargetRegisterClass *SrcRC = 0, *DstRC = 0;
Evan Cheng676dd7c2008-03-11 07:19:34 +0000453 SrcRC = TRI->getPhysicalRegisterRegClass(SrcReg, Node->getValueType(ResNo));
Chris Lattner02b6d252008-03-09 08:49:15 +0000454
Evan Chenga6fb1b62007-09-25 01:54:36 +0000455 // Figure out the register class to create for the destreg.
Chris Lattner02b6d252008-03-09 08:49:15 +0000456 if (VRBase) {
Evan Cheng9e233362008-03-12 22:19:41 +0000457 DstRC = MRI.getRegClass(VRBase);
Chris Lattner02b6d252008-03-09 08:49:15 +0000458 } else {
Evan Cheng8a50f1f2008-04-03 16:36:07 +0000459 DstRC = TLI->getRegClassFor(Node->getValueType(ResNo));
Chris Lattner02b6d252008-03-09 08:49:15 +0000460 }
Evan Chenga6fb1b62007-09-25 01:54:36 +0000461
462 // If all uses are reading from the src physical register and copying the
463 // register is either impossible or very expensive, then don't create a copy.
Chris Lattner02b6d252008-03-09 08:49:15 +0000464 if (MatchReg && SrcRC->getCopyCost() < 0) {
Evan Chenga6fb1b62007-09-25 01:54:36 +0000465 VRBase = SrcReg;
466 } else {
Evan Cheng84097472007-08-02 00:28:15 +0000467 // Create the reg, emit the copy.
Evan Cheng9e233362008-03-12 22:19:41 +0000468 VRBase = MRI.createVirtualRegister(DstRC);
Chris Lattner02b6d252008-03-09 08:49:15 +0000469 TII->copyRegToReg(*BB, BB->end(), VRBase, SrcReg, DstRC, SrcRC);
Evan Cheng84097472007-08-02 00:28:15 +0000470 }
Evan Cheng84097472007-08-02 00:28:15 +0000471
Dan Gohman4c8c8302008-06-21 15:52:51 +0000472 if (IsClone)
Evan Chenga6fb1b62007-09-25 01:54:36 +0000473 VRBaseMap.erase(SDOperand(Node, ResNo));
Evan Cheng84097472007-08-02 00:28:15 +0000474 bool isNew = VRBaseMap.insert(std::make_pair(SDOperand(Node,ResNo), VRBase));
Evan Cheng97e60d92008-05-14 21:08:07 +0000475 isNew = isNew; // Silence compiler warning.
Evan Cheng84097472007-08-02 00:28:15 +0000476 assert(isNew && "Node emitted out of order - early");
477}
478
Evan Cheng8a50f1f2008-04-03 16:36:07 +0000479/// getDstOfCopyToRegUse - If the only use of the specified result number of
480/// node is a CopyToReg, return its destination register. Return 0 otherwise.
481unsigned ScheduleDAG::getDstOfOnlyCopyToRegUse(SDNode *Node,
482 unsigned ResNo) const {
483 if (!Node->hasOneUse())
484 return 0;
485
Roman Levensteindc1adac2008-04-07 10:06:32 +0000486 SDNode *Use = Node->use_begin()->getUser();
Evan Cheng8a50f1f2008-04-03 16:36:07 +0000487 if (Use->getOpcode() == ISD::CopyToReg &&
488 Use->getOperand(2).Val == Node &&
489 Use->getOperand(2).ResNo == ResNo) {
490 unsigned Reg = cast<RegisterSDNode>(Use->getOperand(1))->getReg();
491 if (TargetRegisterInfo::isVirtualRegister(Reg))
492 return Reg;
493 }
494 return 0;
495}
496
Evan Chengda47e6e2008-03-15 00:03:38 +0000497void ScheduleDAG::CreateVirtualRegisters(SDNode *Node, MachineInstr *MI,
Evan Cheng8a50f1f2008-04-03 16:36:07 +0000498 const TargetInstrDesc &II,
Roman Levenstein9cac5252008-04-16 16:15:27 +0000499 DenseMap<SDOperand, unsigned> &VRBaseMap) {
Evan Cheng8a50f1f2008-04-03 16:36:07 +0000500 assert(Node->getTargetOpcode() != TargetInstrInfo::IMPLICIT_DEF &&
501 "IMPLICIT_DEF should have been handled as a special case elsewhere!");
502
Chris Lattner349c4952008-01-07 03:13:06 +0000503 for (unsigned i = 0; i < II.getNumDefs(); ++i) {
Evan Chengaf825c82007-07-10 07:08:32 +0000504 // If the specific node value is only used by a CopyToReg and the dest reg
505 // is a vreg, use the CopyToReg'd destination register instead of creating
506 // a new vreg.
507 unsigned VRBase = 0;
508 for (SDNode::use_iterator UI = Node->use_begin(), E = Node->use_end();
509 UI != E; ++UI) {
Roman Levensteindc1adac2008-04-07 10:06:32 +0000510 SDNode *Use = UI->getUser();
Evan Chengaf825c82007-07-10 07:08:32 +0000511 if (Use->getOpcode() == ISD::CopyToReg &&
512 Use->getOperand(2).Val == Node &&
513 Use->getOperand(2).ResNo == i) {
514 unsigned Reg = cast<RegisterSDNode>(Use->getOperand(1))->getReg();
Dan Gohman6f0d0242008-02-10 18:45:23 +0000515 if (TargetRegisterInfo::isVirtualRegister(Reg)) {
Evan Chengaf825c82007-07-10 07:08:32 +0000516 VRBase = Reg;
Chris Lattner8019f412007-12-30 00:41:17 +0000517 MI->addOperand(MachineOperand::CreateReg(Reg, true));
Evan Chengaf825c82007-07-10 07:08:32 +0000518 break;
519 }
520 }
521 }
522
Evan Cheng84097472007-08-02 00:28:15 +0000523 // Create the result registers for this node and add the result regs to
524 // the machine instruction.
Evan Chengaf825c82007-07-10 07:08:32 +0000525 if (VRBase == 0) {
Evan Cheng8a50f1f2008-04-03 16:36:07 +0000526 const TargetRegisterClass *RC = getInstrOperandRegClass(TRI, TII, II, i);
Evan Chengaf825c82007-07-10 07:08:32 +0000527 assert(RC && "Isn't a register operand!");
Evan Cheng9e233362008-03-12 22:19:41 +0000528 VRBase = MRI.createVirtualRegister(RC);
Chris Lattner8019f412007-12-30 00:41:17 +0000529 MI->addOperand(MachineOperand::CreateReg(VRBase, true));
Evan Chengaf825c82007-07-10 07:08:32 +0000530 }
531
532 bool isNew = VRBaseMap.insert(std::make_pair(SDOperand(Node,i), VRBase));
Evan Cheng97e60d92008-05-14 21:08:07 +0000533 isNew = isNew; // Silence compiler warning.
Evan Chengaf825c82007-07-10 07:08:32 +0000534 assert(isNew && "Node emitted out of order - early");
Jim Laskeye6b90fb2005-09-26 21:57:04 +0000535 }
Jim Laskeye6b90fb2005-09-26 21:57:04 +0000536}
537
Chris Lattnerdf375062006-03-10 07:25:12 +0000538/// getVR - Return the virtual register corresponding to the specified result
539/// of the specified node.
Evan Cheng8a50f1f2008-04-03 16:36:07 +0000540unsigned ScheduleDAG::getVR(SDOperand Op,
Roman Levenstein9cac5252008-04-16 16:15:27 +0000541 DenseMap<SDOperand, unsigned> &VRBaseMap) {
Evan Cheng8a50f1f2008-04-03 16:36:07 +0000542 if (Op.isTargetOpcode() &&
543 Op.getTargetOpcode() == TargetInstrInfo::IMPLICIT_DEF) {
544 // Add an IMPLICIT_DEF instruction before every use.
545 unsigned VReg = getDstOfOnlyCopyToRegUse(Op.Val, Op.ResNo);
546 // IMPLICIT_DEF can produce any type of result so its TargetInstrDesc
547 // does not include operand register class info.
548 if (!VReg) {
549 const TargetRegisterClass *RC = TLI->getRegClassFor(Op.getValueType());
550 VReg = MRI.createVirtualRegister(RC);
551 }
552 BuildMI(BB, TII->get(TargetInstrInfo::IMPLICIT_DEF), VReg);
553 return VReg;
554 }
555
Roman Levenstein9cac5252008-04-16 16:15:27 +0000556 DenseMap<SDOperand, unsigned>::iterator I = VRBaseMap.find(Op);
Chris Lattnerdf375062006-03-10 07:25:12 +0000557 assert(I != VRBaseMap.end() && "Node emitted out of order - late");
Evan Chengaf825c82007-07-10 07:08:32 +0000558 return I->second;
Chris Lattnerdf375062006-03-10 07:25:12 +0000559}
560
561
Chris Lattnered18b682006-02-24 18:54:03 +0000562/// AddOperand - Add the specified operand to the specified machine instr. II
563/// specifies the instruction information for the node, and IIOpNum is the
564/// operand number (in the II) that we are adding. IIOpNum and II are used for
565/// assertions only.
566void ScheduleDAG::AddOperand(MachineInstr *MI, SDOperand Op,
567 unsigned IIOpNum,
Chris Lattner749c6f62008-01-07 07:27:27 +0000568 const TargetInstrDesc *II,
Roman Levenstein9cac5252008-04-16 16:15:27 +0000569 DenseMap<SDOperand, unsigned> &VRBaseMap) {
Chris Lattnered18b682006-02-24 18:54:03 +0000570 if (Op.isTargetOpcode()) {
571 // Note that this case is redundant with the final else block, but we
572 // include it because it is the most common and it makes the logic
573 // simpler here.
574 assert(Op.getValueType() != MVT::Other &&
575 Op.getValueType() != MVT::Flag &&
576 "Chain and flag operands should occur at end of operand list!");
Chris Lattnered18b682006-02-24 18:54:03 +0000577 // Get/emit the operand.
Chris Lattnerdf375062006-03-10 07:25:12 +0000578 unsigned VReg = getVR(Op, VRBaseMap);
Chris Lattner749c6f62008-01-07 07:27:27 +0000579 const TargetInstrDesc &TID = MI->getDesc();
Evan Cheng8a50f1f2008-04-03 16:36:07 +0000580 bool isOptDef = IIOpNum < TID.getNumOperands() &&
581 TID.OpInfo[IIOpNum].isOptionalDef();
Chris Lattner8019f412007-12-30 00:41:17 +0000582 MI->addOperand(MachineOperand::CreateReg(VReg, isOptDef));
Chris Lattnered18b682006-02-24 18:54:03 +0000583
584 // Verify that it is right.
Dan Gohman6f0d0242008-02-10 18:45:23 +0000585 assert(TargetRegisterInfo::isVirtualRegister(VReg) && "Not a vreg?");
Chris Lattnerb7795802008-03-11 00:59:28 +0000586#ifndef NDEBUG
Chris Lattnered18b682006-02-24 18:54:03 +0000587 if (II) {
Chris Lattnerb7795802008-03-11 00:59:28 +0000588 // There may be no register class for this operand if it is a variadic
589 // argument (RC will be NULL in this case). In this case, we just assume
590 // the regclass is ok.
Jim Laskey60f09922006-07-21 20:57:35 +0000591 const TargetRegisterClass *RC =
Dan Gohman6f0d0242008-02-10 18:45:23 +0000592 getInstrOperandRegClass(TRI, TII, *II, IIOpNum);
Chris Lattnerc5733ac2008-03-11 03:14:42 +0000593 assert((RC || II->isVariadic()) && "Expected reg class info!");
Evan Cheng9e233362008-03-12 22:19:41 +0000594 const TargetRegisterClass *VRC = MRI.getRegClass(VReg);
Chris Lattnerb7795802008-03-11 00:59:28 +0000595 if (RC && VRC != RC) {
Chris Lattner01528292007-02-15 18:17:56 +0000596 cerr << "Register class of operand and regclass of use don't agree!\n";
Chris Lattner01528292007-02-15 18:17:56 +0000597 cerr << "Operand = " << IIOpNum << "\n";
Chris Lattner95ad9432007-02-17 06:38:37 +0000598 cerr << "Op->Val = "; Op.Val->dump(&DAG); cerr << "\n";
Chris Lattner01528292007-02-15 18:17:56 +0000599 cerr << "MI = "; MI->print(cerr);
600 cerr << "VReg = " << VReg << "\n";
601 cerr << "VReg RegClass size = " << VRC->getSize()
Chris Lattner5d4a9f72007-02-15 18:19:15 +0000602 << ", align = " << VRC->getAlignment() << "\n";
Chris Lattner01528292007-02-15 18:17:56 +0000603 cerr << "Expected RegClass size = " << RC->getSize()
Chris Lattner5d4a9f72007-02-15 18:19:15 +0000604 << ", align = " << RC->getAlignment() << "\n";
Chris Lattner01528292007-02-15 18:17:56 +0000605 cerr << "Fatal error, aborting.\n";
606 abort();
607 }
Chris Lattnered18b682006-02-24 18:54:03 +0000608 }
Chris Lattnerb7795802008-03-11 00:59:28 +0000609#endif
Chris Lattnerfec65d52007-12-30 00:51:11 +0000610 } else if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Chris Lattner8019f412007-12-30 00:41:17 +0000611 MI->addOperand(MachineOperand::CreateImm(C->getValue()));
Nate Begemane1795842008-02-14 08:57:00 +0000612 } else if (ConstantFPSDNode *F = dyn_cast<ConstantFPSDNode>(Op)) {
Chris Lattner02a260a2008-04-20 00:41:09 +0000613 ConstantFP *CFP = ConstantFP::get(F->getValueAPF());
Nate Begemane1795842008-02-14 08:57:00 +0000614 MI->addOperand(MachineOperand::CreateFPImm(CFP));
Chris Lattnerfec65d52007-12-30 00:51:11 +0000615 } else if (RegisterSDNode *R = dyn_cast<RegisterSDNode>(Op)) {
Chris Lattner8019f412007-12-30 00:41:17 +0000616 MI->addOperand(MachineOperand::CreateReg(R->getReg(), false));
Chris Lattnerfec65d52007-12-30 00:51:11 +0000617 } else if (GlobalAddressSDNode *TGA = dyn_cast<GlobalAddressSDNode>(Op)) {
618 MI->addOperand(MachineOperand::CreateGA(TGA->getGlobal(),TGA->getOffset()));
619 } else if (BasicBlockSDNode *BB = dyn_cast<BasicBlockSDNode>(Op)) {
620 MI->addOperand(MachineOperand::CreateMBB(BB->getBasicBlock()));
621 } else if (FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(Op)) {
622 MI->addOperand(MachineOperand::CreateFI(FI->getIndex()));
623 } else if (JumpTableSDNode *JT = dyn_cast<JumpTableSDNode>(Op)) {
624 MI->addOperand(MachineOperand::CreateJTI(JT->getIndex()));
625 } else if (ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(Op)) {
Evan Cheng404cb4f2006-02-25 09:54:52 +0000626 int Offset = CP->getOffset();
Chris Lattnered18b682006-02-24 18:54:03 +0000627 unsigned Align = CP->getAlignment();
Evan Chengd6594ae2006-09-12 21:00:35 +0000628 const Type *Type = CP->getType();
Chris Lattnered18b682006-02-24 18:54:03 +0000629 // MachineConstantPool wants an explicit alignment.
630 if (Align == 0) {
Evan Chengde268f72007-01-24 07:03:39 +0000631 Align = TM.getTargetData()->getPreferredTypeAlignmentShift(Type);
Evan Chengf6d039a2007-01-22 23:13:55 +0000632 if (Align == 0) {
Reid Spencerac9dcb92007-02-15 03:39:18 +0000633 // Alignment of vector types. FIXME!
Duncan Sands514ab342007-11-01 20:53:16 +0000634 Align = TM.getTargetData()->getABITypeSize(Type);
Evan Chengf6d039a2007-01-22 23:13:55 +0000635 Align = Log2_64(Align);
Chris Lattner54a30b92006-03-20 01:51:46 +0000636 }
Chris Lattnered18b682006-02-24 18:54:03 +0000637 }
638
Evan Chengd6594ae2006-09-12 21:00:35 +0000639 unsigned Idx;
640 if (CP->isMachineConstantPoolEntry())
641 Idx = ConstPool->getConstantPoolIndex(CP->getMachineCPVal(), Align);
642 else
643 Idx = ConstPool->getConstantPoolIndex(CP->getConstVal(), Align);
Chris Lattnerfec65d52007-12-30 00:51:11 +0000644 MI->addOperand(MachineOperand::CreateCPI(Idx, Offset));
645 } else if (ExternalSymbolSDNode *ES = dyn_cast<ExternalSymbolSDNode>(Op)) {
646 MI->addOperand(MachineOperand::CreateES(ES->getSymbol()));
Chris Lattnered18b682006-02-24 18:54:03 +0000647 } else {
648 assert(Op.getValueType() != MVT::Other &&
649 Op.getValueType() != MVT::Flag &&
650 "Chain and flag operands should occur at end of operand list!");
Chris Lattnerdf375062006-03-10 07:25:12 +0000651 unsigned VReg = getVR(Op, VRBaseMap);
Chris Lattner8019f412007-12-30 00:41:17 +0000652 MI->addOperand(MachineOperand::CreateReg(VReg, false));
Chris Lattnered18b682006-02-24 18:54:03 +0000653
Chris Lattner02b6d252008-03-09 08:49:15 +0000654 // Verify that it is right. Note that the reg class of the physreg and the
655 // vreg don't necessarily need to match, but the target copy insertion has
656 // to be able to handle it. This handles things like copies from ST(0) to
657 // an FP vreg on x86.
Dan Gohman6f0d0242008-02-10 18:45:23 +0000658 assert(TargetRegisterInfo::isVirtualRegister(VReg) && "Not a vreg?");
Chris Lattnerc5733ac2008-03-11 03:14:42 +0000659 if (II && !II->isVariadic()) {
Chris Lattner02b6d252008-03-09 08:49:15 +0000660 assert(getInstrOperandRegClass(TRI, TII, *II, IIOpNum) &&
661 "Don't have operand info for this instruction!");
Chris Lattnered18b682006-02-24 18:54:03 +0000662 }
663 }
664
665}
666
Dan Gohman36b5c132008-04-07 19:35:22 +0000667void ScheduleDAG::AddMemOperand(MachineInstr *MI, const MachineMemOperand &MO) {
Dan Gohman69de1932008-02-06 22:27:42 +0000668 MI->addMemOperand(MO);
669}
670
Christopher Lambe24f8f12007-07-26 08:12:07 +0000671// Returns the Register Class of a subregister
672static const TargetRegisterClass *getSubRegisterRegClass(
673 const TargetRegisterClass *TRC,
674 unsigned SubIdx) {
675 // Pick the register class of the subregister
Dan Gohman6f0d0242008-02-10 18:45:23 +0000676 TargetRegisterInfo::regclass_iterator I =
677 TRC->subregclasses_begin() + SubIdx-1;
Christopher Lambe24f8f12007-07-26 08:12:07 +0000678 assert(I < TRC->subregclasses_end() &&
679 "Invalid subregister index for register class");
680 return *I;
681}
682
683static const TargetRegisterClass *getSuperregRegisterClass(
684 const TargetRegisterClass *TRC,
685 unsigned SubIdx,
Duncan Sands83ec4b62008-06-06 12:08:01 +0000686 MVT VT) {
Christopher Lambe24f8f12007-07-26 08:12:07 +0000687 // Pick the register class of the superegister for this type
Dan Gohman6f0d0242008-02-10 18:45:23 +0000688 for (TargetRegisterInfo::regclass_iterator I = TRC->superregclasses_begin(),
Christopher Lambe24f8f12007-07-26 08:12:07 +0000689 E = TRC->superregclasses_end(); I != E; ++I)
690 if ((*I)->hasType(VT) && getSubRegisterRegClass(*I, SubIdx) == TRC)
691 return *I;
692 assert(false && "Couldn't find the register class");
693 return 0;
694}
695
696/// EmitSubregNode - Generate machine code for subreg nodes.
697///
698void ScheduleDAG::EmitSubregNode(SDNode *Node,
Roman Levenstein9cac5252008-04-16 16:15:27 +0000699 DenseMap<SDOperand, unsigned> &VRBaseMap) {
Christopher Lambe24f8f12007-07-26 08:12:07 +0000700 unsigned VRBase = 0;
701 unsigned Opc = Node->getTargetOpcode();
Christopher Lambc9298232008-03-16 03:12:01 +0000702
703 // If the node is only used by a CopyToReg and the dest reg is a vreg, use
704 // the CopyToReg'd destination register instead of creating a new vreg.
705 for (SDNode::use_iterator UI = Node->use_begin(), E = Node->use_end();
706 UI != E; ++UI) {
Roman Levensteindc1adac2008-04-07 10:06:32 +0000707 SDNode *Use = UI->getUser();
Christopher Lambc9298232008-03-16 03:12:01 +0000708 if (Use->getOpcode() == ISD::CopyToReg &&
709 Use->getOperand(2).Val == Node) {
710 unsigned DestReg = cast<RegisterSDNode>(Use->getOperand(1))->getReg();
711 if (TargetRegisterInfo::isVirtualRegister(DestReg)) {
712 VRBase = DestReg;
713 break;
Christopher Lambe24f8f12007-07-26 08:12:07 +0000714 }
715 }
Christopher Lambc9298232008-03-16 03:12:01 +0000716 }
717
718 if (Opc == TargetInstrInfo::EXTRACT_SUBREG) {
Christopher Lambe24f8f12007-07-26 08:12:07 +0000719 unsigned SubIdx = cast<ConstantSDNode>(Node->getOperand(1))->getValue();
Christopher Lambe24f8f12007-07-26 08:12:07 +0000720
Christopher Lambe24f8f12007-07-26 08:12:07 +0000721 // Create the extract_subreg machine instruction.
Evan Cheng8a50f1f2008-04-03 16:36:07 +0000722 MachineInstr *MI = BuildMI(TII->get(TargetInstrInfo::EXTRACT_SUBREG));
Christopher Lambe24f8f12007-07-26 08:12:07 +0000723
724 // Figure out the register class to create for the destreg.
725 unsigned VReg = getVR(Node->getOperand(0), VRBaseMap);
Evan Cheng9e233362008-03-12 22:19:41 +0000726 const TargetRegisterClass *TRC = MRI.getRegClass(VReg);
Christopher Lambe24f8f12007-07-26 08:12:07 +0000727 const TargetRegisterClass *SRC = getSubRegisterRegClass(TRC, SubIdx);
728
729 if (VRBase) {
730 // Grab the destination register
Evan Cheng50871242008-05-14 20:07:51 +0000731#ifndef NDEBUG
Evan Cheng9e233362008-03-12 22:19:41 +0000732 const TargetRegisterClass *DRC = MRI.getRegClass(VRBase);
Christopher Lamb175e8152008-01-31 07:09:08 +0000733 assert(SRC && DRC && SRC == DRC &&
Christopher Lambe24f8f12007-07-26 08:12:07 +0000734 "Source subregister and destination must have the same class");
Evan Cheng50871242008-05-14 20:07:51 +0000735#endif
Christopher Lambe24f8f12007-07-26 08:12:07 +0000736 } else {
737 // Create the reg
Christopher Lamb175e8152008-01-31 07:09:08 +0000738 assert(SRC && "Couldn't find source register class");
Evan Cheng9e233362008-03-12 22:19:41 +0000739 VRBase = MRI.createVirtualRegister(SRC);
Christopher Lambe24f8f12007-07-26 08:12:07 +0000740 }
741
742 // Add def, source, and subreg index
Chris Lattner8019f412007-12-30 00:41:17 +0000743 MI->addOperand(MachineOperand::CreateReg(VRBase, true));
Christopher Lambe24f8f12007-07-26 08:12:07 +0000744 AddOperand(MI, Node->getOperand(0), 0, 0, VRBaseMap);
Chris Lattnerfec65d52007-12-30 00:51:11 +0000745 MI->addOperand(MachineOperand::CreateImm(SubIdx));
Evan Cheng8a50f1f2008-04-03 16:36:07 +0000746 BB->push_back(MI);
Christopher Lambc9298232008-03-16 03:12:01 +0000747 } else if (Opc == TargetInstrInfo::INSERT_SUBREG ||
748 Opc == TargetInstrInfo::SUBREG_TO_REG) {
Christopher Lamb1fab4a62008-03-11 10:09:17 +0000749 SDOperand N0 = Node->getOperand(0);
750 SDOperand N1 = Node->getOperand(1);
751 SDOperand N2 = Node->getOperand(2);
752 unsigned SubReg = getVR(N1, VRBaseMap);
753 unsigned SubIdx = cast<ConstantSDNode>(N2)->getValue();
Christopher Lambe24f8f12007-07-26 08:12:07 +0000754
Christopher Lambe24f8f12007-07-26 08:12:07 +0000755
756 // Figure out the register class to create for the destreg.
757 const TargetRegisterClass *TRC = 0;
758 if (VRBase) {
Evan Cheng9e233362008-03-12 22:19:41 +0000759 TRC = MRI.getRegClass(VRBase);
Christopher Lambe24f8f12007-07-26 08:12:07 +0000760 } else {
Evan Cheng9e233362008-03-12 22:19:41 +0000761 TRC = getSuperregRegisterClass(MRI.getRegClass(SubReg), SubIdx,
Christopher Lambe24f8f12007-07-26 08:12:07 +0000762 Node->getValueType(0));
763 assert(TRC && "Couldn't determine register class for insert_subreg");
Evan Cheng9e233362008-03-12 22:19:41 +0000764 VRBase = MRI.createVirtualRegister(TRC); // Create the reg
Christopher Lambe24f8f12007-07-26 08:12:07 +0000765 }
766
Christopher Lambc9298232008-03-16 03:12:01 +0000767 // Create the insert_subreg or subreg_to_reg machine instruction.
Evan Cheng8a50f1f2008-04-03 16:36:07 +0000768 MachineInstr *MI = BuildMI(TII->get(Opc));
Chris Lattner8019f412007-12-30 00:41:17 +0000769 MI->addOperand(MachineOperand::CreateReg(VRBase, true));
Christopher Lamb1fab4a62008-03-11 10:09:17 +0000770
Christopher Lambc9298232008-03-16 03:12:01 +0000771 // If creating a subreg_to_reg, then the first input operand
772 // is an implicit value immediate, otherwise it's a register
773 if (Opc == TargetInstrInfo::SUBREG_TO_REG) {
774 const ConstantSDNode *SD = cast<ConstantSDNode>(N0);
Christopher Lamb1fab4a62008-03-11 10:09:17 +0000775 MI->addOperand(MachineOperand::CreateImm(SD->getValue()));
Christopher Lambc9298232008-03-16 03:12:01 +0000776 } else
Christopher Lamb1fab4a62008-03-11 10:09:17 +0000777 AddOperand(MI, N0, 0, 0, VRBaseMap);
778 // Add the subregster being inserted
779 AddOperand(MI, N1, 0, 0, VRBaseMap);
Chris Lattnerfec65d52007-12-30 00:51:11 +0000780 MI->addOperand(MachineOperand::CreateImm(SubIdx));
Evan Cheng8a50f1f2008-04-03 16:36:07 +0000781 BB->push_back(MI);
Christopher Lambe24f8f12007-07-26 08:12:07 +0000782 } else
Christopher Lambc9298232008-03-16 03:12:01 +0000783 assert(0 && "Node is not insert_subreg, extract_subreg, or subreg_to_reg");
Christopher Lambe24f8f12007-07-26 08:12:07 +0000784
785 bool isNew = VRBaseMap.insert(std::make_pair(SDOperand(Node,0), VRBase));
Evan Cheng97e60d92008-05-14 21:08:07 +0000786 isNew = isNew; // Silence compiler warning.
Christopher Lambe24f8f12007-07-26 08:12:07 +0000787 assert(isNew && "Node emitted out of order - early");
788}
789
Jim Laskeyb6d4c2c2005-09-30 19:15:27 +0000790/// EmitNode - Generate machine code for an node and needed dependencies.
Jim Laskeye6b90fb2005-09-26 21:57:04 +0000791///
Dan Gohman4c8c8302008-06-21 15:52:51 +0000792void ScheduleDAG::EmitNode(SDNode *Node, bool IsClone,
Roman Levenstein9cac5252008-04-16 16:15:27 +0000793 DenseMap<SDOperand, unsigned> &VRBaseMap) {
Jim Laskeyb6d4c2c2005-09-30 19:15:27 +0000794 // If machine instruction
795 if (Node->isTargetOpcode()) {
796 unsigned Opc = Node->getTargetOpcode();
Christopher Lambe24f8f12007-07-26 08:12:07 +0000797
798 // Handle subreg insert/extract specially
799 if (Opc == TargetInstrInfo::EXTRACT_SUBREG ||
Christopher Lambc9298232008-03-16 03:12:01 +0000800 Opc == TargetInstrInfo::INSERT_SUBREG ||
801 Opc == TargetInstrInfo::SUBREG_TO_REG) {
Christopher Lambe24f8f12007-07-26 08:12:07 +0000802 EmitSubregNode(Node, VRBaseMap);
803 return;
804 }
Evan Cheng8a50f1f2008-04-03 16:36:07 +0000805
806 if (Opc == TargetInstrInfo::IMPLICIT_DEF)
807 // We want a unique VR for each IMPLICIT_DEF use.
808 return;
Christopher Lambe24f8f12007-07-26 08:12:07 +0000809
Chris Lattner749c6f62008-01-07 07:27:27 +0000810 const TargetInstrDesc &II = TII->get(Opc);
Jim Laskeyb6d4c2c2005-09-30 19:15:27 +0000811 unsigned NumResults = CountResults(Node);
812 unsigned NodeOperands = CountOperands(Node);
Dan Gohman42a77882008-02-16 00:36:48 +0000813 unsigned MemOperandsEnd = ComputeMemOperandsEnd(Node);
Chris Lattner349c4952008-01-07 03:13:06 +0000814 bool HasPhysRegOuts = (NumResults > II.getNumDefs()) &&
815 II.getImplicitDefs() != 0;
Chris Lattnerda8abb02005-09-01 18:44:10 +0000816#ifndef NDEBUG
Evan Cheng50871242008-05-14 20:07:51 +0000817 unsigned NumMIOperands = NodeOperands + NumResults;
Chris Lattner349c4952008-01-07 03:13:06 +0000818 assert((II.getNumOperands() == NumMIOperands ||
Chris Lattner8f707e12008-01-07 05:19:29 +0000819 HasPhysRegOuts || II.isVariadic()) &&
Chris Lattner2d973e42005-08-18 20:07:59 +0000820 "#operands for dag node doesn't match .td file!");
Chris Lattnerca6aa2f2005-08-19 01:01:34 +0000821#endif
Chris Lattner2d973e42005-08-18 20:07:59 +0000822
823 // Create the new machine instruction.
Evan Cheng8a50f1f2008-04-03 16:36:07 +0000824 MachineInstr *MI = BuildMI(II);
Chris Lattner2d973e42005-08-18 20:07:59 +0000825
826 // Add result register values for things that are defined by this
827 // instruction.
Evan Chengaf825c82007-07-10 07:08:32 +0000828 if (NumResults)
Evan Cheng84097472007-08-02 00:28:15 +0000829 CreateVirtualRegisters(Node, MI, II, VRBaseMap);
Jim Laskeyb6d4c2c2005-09-30 19:15:27 +0000830
831 // Emit all of the actual operands of this instruction, adding them to the
832 // instruction as appropriate.
Chris Lattnered18b682006-02-24 18:54:03 +0000833 for (unsigned i = 0; i != NodeOperands; ++i)
Chris Lattner349c4952008-01-07 03:13:06 +0000834 AddOperand(MI, Node->getOperand(i), i+II.getNumDefs(), &II, VRBaseMap);
Evan Cheng13d41b92006-05-12 01:58:24 +0000835
Dan Gohman69de1932008-02-06 22:27:42 +0000836 // Emit all of the memory operands of this instruction
Dan Gohman42a77882008-02-16 00:36:48 +0000837 for (unsigned i = NodeOperands; i != MemOperandsEnd; ++i)
Dan Gohman69de1932008-02-06 22:27:42 +0000838 AddMemOperand(MI, cast<MemOperandSDNode>(Node->getOperand(i))->MO);
839
Evan Cheng13d41b92006-05-12 01:58:24 +0000840 // Commute node if it has been determined to be profitable.
841 if (CommuteSet.count(Node)) {
842 MachineInstr *NewMI = TII->commuteInstruction(MI);
843 if (NewMI == 0)
Bill Wendling832171c2006-12-07 20:04:42 +0000844 DOUT << "Sched: COMMUTING FAILED!\n";
Evan Cheng13d41b92006-05-12 01:58:24 +0000845 else {
Bill Wendling832171c2006-12-07 20:04:42 +0000846 DOUT << "Sched: COMMUTED TO: " << *NewMI;
Evan Cheng4c6f2f92006-05-31 18:03:39 +0000847 if (MI != NewMI) {
848 delete MI;
849 MI = NewMI;
850 }
Evan Cheng643afa52008-02-28 07:40:24 +0000851 ++NumCommutes;
Evan Cheng13d41b92006-05-12 01:58:24 +0000852 }
853 }
854
Evan Cheng1b08bbc2008-02-01 09:10:45 +0000855 if (II.usesCustomDAGSchedInsertionHook())
Evan Cheng6b2cf282008-01-30 19:35:32 +0000856 // Insert this instruction into the basic block using a target
857 // specific inserter which may returns a new basic block.
Evan Cheng8a50f1f2008-04-03 16:36:07 +0000858 BB = TLI->EmitInstrWithCustomInserter(MI, BB);
Evan Cheng6b2cf282008-01-30 19:35:32 +0000859 else
860 BB->push_back(MI);
Evan Cheng84097472007-08-02 00:28:15 +0000861
862 // Additional results must be an physical register def.
863 if (HasPhysRegOuts) {
Chris Lattner349c4952008-01-07 03:13:06 +0000864 for (unsigned i = II.getNumDefs(); i < NumResults; ++i) {
865 unsigned Reg = II.getImplicitDefs()[i - II.getNumDefs()];
Evan Cheng33d55952007-08-02 05:29:38 +0000866 if (Node->hasAnyUseOfValue(i))
Dan Gohman4c8c8302008-06-21 15:52:51 +0000867 EmitCopyFromReg(Node, i, IsClone, Reg, VRBaseMap);
Evan Cheng84097472007-08-02 00:28:15 +0000868 }
869 }
Jim Laskeyb6d4c2c2005-09-30 19:15:27 +0000870 } else {
871 switch (Node->getOpcode()) {
872 default:
Jim Laskey16d42c62006-07-11 18:25:13 +0000873#ifndef NDEBUG
Dan Gohmanb5bec2b2007-06-19 14:13:56 +0000874 Node->dump(&DAG);
Jim Laskey16d42c62006-07-11 18:25:13 +0000875#endif
Jim Laskeyb6d4c2c2005-09-30 19:15:27 +0000876 assert(0 && "This target-independent node should have been selected!");
Dan Gohman80792f32008-04-15 01:22:18 +0000877 break;
878 case ISD::EntryToken:
879 assert(0 && "EntryToken should have been excluded from the schedule!");
880 break;
881 case ISD::TokenFactor: // fall thru
Evan Chenga844bde2008-02-02 04:07:54 +0000882 case ISD::DECLARE:
Dan Gohman69de1932008-02-06 22:27:42 +0000883 case ISD::SRCVALUE:
Jim Laskeyb6d4c2c2005-09-30 19:15:27 +0000884 break;
Dan Gohman44066042008-07-01 00:05:16 +0000885 case ISD::DBG_LABEL:
886 BB->push_back(BuildMI(TII->get(TargetInstrInfo::DBG_LABEL))
887 .addImm(cast<LabelSDNode>(Node)->getLabelID()));
888 break;
889 case ISD::EH_LABEL:
890 BB->push_back(BuildMI(TII->get(TargetInstrInfo::EH_LABEL))
891 .addImm(cast<LabelSDNode>(Node)->getLabelID()));
892 break;
Jim Laskeyb6d4c2c2005-09-30 19:15:27 +0000893 case ISD::CopyToReg: {
Chris Lattnerf30e1cf2008-03-09 09:15:31 +0000894 unsigned SrcReg;
895 SDOperand SrcVal = Node->getOperand(2);
896 if (RegisterSDNode *R = dyn_cast<RegisterSDNode>(SrcVal))
897 SrcReg = R->getReg();
Evan Cheng489a87c2007-01-05 20:59:06 +0000898 else
Chris Lattnerf30e1cf2008-03-09 09:15:31 +0000899 SrcReg = getVR(SrcVal, VRBaseMap);
900
Chris Lattnera4176522005-10-30 18:54:27 +0000901 unsigned DestReg = cast<RegisterSDNode>(Node->getOperand(1))->getReg();
Chris Lattnerf30e1cf2008-03-09 09:15:31 +0000902 if (SrcReg == DestReg) // Coalesced away the copy? Ignore.
903 break;
904
905 const TargetRegisterClass *SrcTRC = 0, *DstTRC = 0;
906 // Get the register classes of the src/dst.
907 if (TargetRegisterInfo::isVirtualRegister(SrcReg))
Evan Cheng9e233362008-03-12 22:19:41 +0000908 SrcTRC = MRI.getRegClass(SrcReg);
Chris Lattnerf30e1cf2008-03-09 09:15:31 +0000909 else
Evan Cheng676dd7c2008-03-11 07:19:34 +0000910 SrcTRC = TRI->getPhysicalRegisterRegClass(SrcReg,SrcVal.getValueType());
Chris Lattnerf30e1cf2008-03-09 09:15:31 +0000911
912 if (TargetRegisterInfo::isVirtualRegister(DestReg))
Evan Cheng9e233362008-03-12 22:19:41 +0000913 DstTRC = MRI.getRegClass(DestReg);
Chris Lattnerf30e1cf2008-03-09 09:15:31 +0000914 else
Evan Cheng676dd7c2008-03-11 07:19:34 +0000915 DstTRC = TRI->getPhysicalRegisterRegClass(DestReg,
916 Node->getOperand(1).getValueType());
Chris Lattnerf30e1cf2008-03-09 09:15:31 +0000917 TII->copyRegToReg(*BB, BB->end(), DestReg, SrcReg, DstTRC, SrcTRC);
Jim Laskeyb6d4c2c2005-09-30 19:15:27 +0000918 break;
919 }
920 case ISD::CopyFromReg: {
921 unsigned SrcReg = cast<RegisterSDNode>(Node->getOperand(1))->getReg();
Dan Gohman4c8c8302008-06-21 15:52:51 +0000922 EmitCopyFromReg(Node, 0, IsClone, SrcReg, VRBaseMap);
Jim Laskeyb6d4c2c2005-09-30 19:15:27 +0000923 break;
924 }
Chris Lattneracc43bf2006-01-26 23:28:04 +0000925 case ISD::INLINEASM: {
926 unsigned NumOps = Node->getNumOperands();
927 if (Node->getOperand(NumOps-1).getValueType() == MVT::Flag)
928 --NumOps; // Ignore the flag operand.
929
930 // Create the inline asm machine instruction.
Evan Cheng8a50f1f2008-04-03 16:36:07 +0000931 MachineInstr *MI = BuildMI(TII->get(TargetInstrInfo::INLINEASM));
Chris Lattneracc43bf2006-01-26 23:28:04 +0000932
933 // Add the asm string as an external symbol operand.
934 const char *AsmStr =
935 cast<ExternalSymbolSDNode>(Node->getOperand(1))->getSymbol();
Chris Lattnerfec65d52007-12-30 00:51:11 +0000936 MI->addOperand(MachineOperand::CreateES(AsmStr));
Chris Lattneracc43bf2006-01-26 23:28:04 +0000937
938 // Add all of the operand registers to the instruction.
Chris Lattnerc3a9f8d2006-02-23 19:21:04 +0000939 for (unsigned i = 2; i != NumOps;) {
940 unsigned Flags = cast<ConstantSDNode>(Node->getOperand(i))->getValue();
Chris Lattnerfd6d2822006-02-24 19:18:20 +0000941 unsigned NumVals = Flags >> 3;
Chris Lattnerc3a9f8d2006-02-23 19:21:04 +0000942
Chris Lattnerfec65d52007-12-30 00:51:11 +0000943 MI->addOperand(MachineOperand::CreateImm(Flags));
Chris Lattnerc3a9f8d2006-02-23 19:21:04 +0000944 ++i; // Skip the ID value.
945
946 switch (Flags & 7) {
Chris Lattneracc43bf2006-01-26 23:28:04 +0000947 default: assert(0 && "Bad flags!");
Chris Lattnerc3a9f8d2006-02-23 19:21:04 +0000948 case 1: // Use of register.
Chris Lattnerfd6d2822006-02-24 19:18:20 +0000949 for (; NumVals; --NumVals, ++i) {
Chris Lattnerc3a9f8d2006-02-23 19:21:04 +0000950 unsigned Reg = cast<RegisterSDNode>(Node->getOperand(i))->getReg();
Chris Lattner8019f412007-12-30 00:41:17 +0000951 MI->addOperand(MachineOperand::CreateReg(Reg, false));
Chris Lattnerc3a9f8d2006-02-23 19:21:04 +0000952 }
Chris Lattnerdc19b702006-02-04 02:26:14 +0000953 break;
Chris Lattnerc3a9f8d2006-02-23 19:21:04 +0000954 case 2: // Def of register.
Chris Lattnerfd6d2822006-02-24 19:18:20 +0000955 for (; NumVals; --NumVals, ++i) {
Chris Lattnerc3a9f8d2006-02-23 19:21:04 +0000956 unsigned Reg = cast<RegisterSDNode>(Node->getOperand(i))->getReg();
Chris Lattner8019f412007-12-30 00:41:17 +0000957 MI->addOperand(MachineOperand::CreateReg(Reg, true));
Chris Lattnerc3a9f8d2006-02-23 19:21:04 +0000958 }
Chris Lattnerdc19b702006-02-04 02:26:14 +0000959 break;
Chris Lattnerdc19b702006-02-04 02:26:14 +0000960 case 3: { // Immediate.
Chris Lattner7df31dc2007-08-25 00:53:07 +0000961 for (; NumVals; --NumVals, ++i) {
962 if (ConstantSDNode *CS =
963 dyn_cast<ConstantSDNode>(Node->getOperand(i))) {
Chris Lattner8019f412007-12-30 00:41:17 +0000964 MI->addOperand(MachineOperand::CreateImm(CS->getValue()));
Dale Johanneseneb57ea72007-11-05 21:20:28 +0000965 } else if (GlobalAddressSDNode *GA =
966 dyn_cast<GlobalAddressSDNode>(Node->getOperand(i))) {
Chris Lattnerfec65d52007-12-30 00:51:11 +0000967 MI->addOperand(MachineOperand::CreateGA(GA->getGlobal(),
968 GA->getOffset()));
Dale Johanneseneb57ea72007-11-05 21:20:28 +0000969 } else {
Chris Lattnerfec65d52007-12-30 00:51:11 +0000970 BasicBlockSDNode *BB =cast<BasicBlockSDNode>(Node->getOperand(i));
971 MI->addOperand(MachineOperand::CreateMBB(BB->getBasicBlock()));
Chris Lattner7df31dc2007-08-25 00:53:07 +0000972 }
Chris Lattnerefa46ce2006-10-31 20:01:56 +0000973 }
Chris Lattnerdc19b702006-02-04 02:26:14 +0000974 break;
975 }
Chris Lattnerfd6d2822006-02-24 19:18:20 +0000976 case 4: // Addressing mode.
977 // The addressing mode has been selected, just add all of the
978 // operands to the machine instruction.
979 for (; NumVals; --NumVals, ++i)
Chris Lattnerdf375062006-03-10 07:25:12 +0000980 AddOperand(MI, Node->getOperand(i), 0, 0, VRBaseMap);
Chris Lattnerfd6d2822006-02-24 19:18:20 +0000981 break;
Chris Lattnerdc19b702006-02-04 02:26:14 +0000982 }
Chris Lattneracc43bf2006-01-26 23:28:04 +0000983 }
Evan Cheng8a50f1f2008-04-03 16:36:07 +0000984 BB->push_back(MI);
Chris Lattneracc43bf2006-01-26 23:28:04 +0000985 break;
986 }
Jim Laskeyb6d4c2c2005-09-30 19:15:27 +0000987 }
988 }
Jim Laskeyb6d4c2c2005-09-30 19:15:27 +0000989}
990
Chris Lattnera93dfcd2006-03-05 23:51:47 +0000991void ScheduleDAG::EmitNoop() {
992 TII->insertNoop(*BB, BB->end());
993}
994
Chris Lattnerd9c4c452008-03-09 07:51:01 +0000995void ScheduleDAG::EmitCrossRCCopy(SUnit *SU,
996 DenseMap<SUnit*, unsigned> &VRBaseMap) {
Evan Cheng42d60272007-09-26 21:36:17 +0000997 for (SUnit::const_pred_iterator I = SU->Preds.begin(), E = SU->Preds.end();
998 I != E; ++I) {
999 if (I->isCtrl) continue; // ignore chain preds
1000 if (!I->Dep->Node) {
1001 // Copy to physical register.
1002 DenseMap<SUnit*, unsigned>::iterator VRI = VRBaseMap.find(I->Dep);
1003 assert(VRI != VRBaseMap.end() && "Node emitted out of order - late");
1004 // Find the destination physical register.
1005 unsigned Reg = 0;
1006 for (SUnit::const_succ_iterator II = SU->Succs.begin(),
1007 EE = SU->Succs.end(); II != EE; ++II) {
1008 if (I->Reg) {
1009 Reg = I->Reg;
1010 break;
1011 }
1012 }
1013 assert(I->Reg && "Unknown physical register!");
Owen Andersond10fd972007-12-31 06:32:00 +00001014 TII->copyRegToReg(*BB, BB->end(), Reg, VRI->second,
Evan Cheng42d60272007-09-26 21:36:17 +00001015 SU->CopyDstRC, SU->CopySrcRC);
1016 } else {
1017 // Copy from physical register.
1018 assert(I->Reg && "Unknown physical register!");
Evan Cheng9e233362008-03-12 22:19:41 +00001019 unsigned VRBase = MRI.createVirtualRegister(SU->CopyDstRC);
Evan Cheng42d60272007-09-26 21:36:17 +00001020 bool isNew = VRBaseMap.insert(std::make_pair(SU, VRBase));
Evan Cheng97e60d92008-05-14 21:08:07 +00001021 isNew = isNew; // Silence compiler warning.
Evan Cheng42d60272007-09-26 21:36:17 +00001022 assert(isNew && "Node emitted out of order - early");
Owen Andersond10fd972007-12-31 06:32:00 +00001023 TII->copyRegToReg(*BB, BB->end(), VRBase, I->Reg,
Evan Cheng42d60272007-09-26 21:36:17 +00001024 SU->CopyDstRC, SU->CopySrcRC);
1025 }
1026 break;
1027 }
1028}
1029
Evan Cheng9e233362008-03-12 22:19:41 +00001030/// EmitLiveInCopy - Emit a copy for a live in physical register. If the
1031/// physical register has only a single copy use, then coalesced the copy
Evan Chengdb2d7732008-03-14 00:14:55 +00001032/// if possible.
1033void ScheduleDAG::EmitLiveInCopy(MachineBasicBlock *MBB,
1034 MachineBasicBlock::iterator &InsertPos,
1035 unsigned VirtReg, unsigned PhysReg,
1036 const TargetRegisterClass *RC,
1037 DenseMap<MachineInstr*, unsigned> &CopyRegMap){
Evan Cheng9e233362008-03-12 22:19:41 +00001038 unsigned NumUses = 0;
1039 MachineInstr *UseMI = NULL;
1040 for (MachineRegisterInfo::use_iterator UI = MRI.use_begin(VirtReg),
1041 UE = MRI.use_end(); UI != UE; ++UI) {
1042 UseMI = &*UI;
1043 if (++NumUses > 1)
1044 break;
1045 }
1046
1047 // If the number of uses is not one, or the use is not a move instruction,
Evan Chengdb2d7732008-03-14 00:14:55 +00001048 // don't coalesce. Also, only coalesce away a virtual register to virtual
1049 // register copy.
1050 bool Coalesced = false;
Evan Cheng9e233362008-03-12 22:19:41 +00001051 unsigned SrcReg, DstReg;
Evan Chengdb2d7732008-03-14 00:14:55 +00001052 if (NumUses == 1 &&
1053 TII->isMoveInstr(*UseMI, SrcReg, DstReg) &&
1054 TargetRegisterInfo::isVirtualRegister(DstReg)) {
1055 VirtReg = DstReg;
1056 Coalesced = true;
Evan Cheng9e233362008-03-12 22:19:41 +00001057 }
1058
Evan Chengdb2d7732008-03-14 00:14:55 +00001059 // Now find an ideal location to insert the copy.
1060 MachineBasicBlock::iterator Pos = InsertPos;
1061 while (Pos != MBB->begin()) {
1062 MachineInstr *PrevMI = prior(Pos);
1063 DenseMap<MachineInstr*, unsigned>::iterator RI = CopyRegMap.find(PrevMI);
1064 // copyRegToReg might emit multiple instructions to do a copy.
1065 unsigned CopyDstReg = (RI == CopyRegMap.end()) ? 0 : RI->second;
1066 if (CopyDstReg && !TRI->regsOverlap(CopyDstReg, PhysReg))
1067 // This is what the BB looks like right now:
1068 // r1024 = mov r0
1069 // ...
1070 // r1 = mov r1024
1071 //
1072 // We want to insert "r1025 = mov r1". Inserting this copy below the
1073 // move to r1024 makes it impossible for that move to be coalesced.
1074 //
1075 // r1025 = mov r1
1076 // r1024 = mov r0
1077 // ...
1078 // r1 = mov 1024
1079 // r2 = mov 1025
1080 break; // Woot! Found a good location.
1081 --Pos;
1082 }
1083
1084 TII->copyRegToReg(*MBB, Pos, VirtReg, PhysReg, RC, RC);
1085 CopyRegMap.insert(std::make_pair(prior(Pos), VirtReg));
1086 if (Coalesced) {
Evan Cheng9e233362008-03-12 22:19:41 +00001087 if (&*InsertPos == UseMI) ++InsertPos;
1088 MBB->erase(UseMI);
Evan Cheng9e233362008-03-12 22:19:41 +00001089 }
Evan Cheng9e233362008-03-12 22:19:41 +00001090}
1091
1092/// EmitLiveInCopies - If this is the first basic block in the function,
1093/// and if it has live ins that need to be copied into vregs, emit the
1094/// copies into the top of the block.
1095void ScheduleDAG::EmitLiveInCopies(MachineBasicBlock *MBB) {
Evan Chengdb2d7732008-03-14 00:14:55 +00001096 DenseMap<MachineInstr*, unsigned> CopyRegMap;
Evan Cheng9e233362008-03-12 22:19:41 +00001097 MachineBasicBlock::iterator InsertPos = MBB->begin();
1098 for (MachineRegisterInfo::livein_iterator LI = MRI.livein_begin(),
1099 E = MRI.livein_end(); LI != E; ++LI)
1100 if (LI->second) {
1101 const TargetRegisterClass *RC = MRI.getRegClass(LI->second);
Evan Chengdb2d7732008-03-14 00:14:55 +00001102 EmitLiveInCopy(MBB, InsertPos, LI->second, LI->first, RC, CopyRegMap);
Evan Cheng9e233362008-03-12 22:19:41 +00001103 }
1104}
1105
Evan Chenge165a782006-05-11 23:55:42 +00001106/// EmitSchedule - Emit the machine code in scheduled order.
1107void ScheduleDAG::EmitSchedule() {
Evan Cheng9e233362008-03-12 22:19:41 +00001108 bool isEntryBB = &MF->front() == BB;
1109
1110 if (isEntryBB && !SchedLiveInCopies) {
1111 // If this is the first basic block in the function, and if it has live ins
1112 // that need to be copied into vregs, emit the copies into the top of the
1113 // block before emitting the code for the block.
1114 for (MachineRegisterInfo::livein_iterator LI = MRI.livein_begin(),
1115 E = MRI.livein_end(); LI != E; ++LI)
Evan Cheng9efce632007-09-26 06:25:56 +00001116 if (LI->second) {
Evan Cheng9e233362008-03-12 22:19:41 +00001117 const TargetRegisterClass *RC = MRI.getRegClass(LI->second);
Evan Cheng6b2cf282008-01-30 19:35:32 +00001118 TII->copyRegToReg(*MF->begin(), MF->begin()->end(), LI->second,
Evan Cheng9efce632007-09-26 06:25:56 +00001119 LI->first, RC, RC);
1120 }
Chris Lattner96645412006-05-16 06:10:58 +00001121 }
Evan Cheng9e233362008-03-12 22:19:41 +00001122
Chris Lattner96645412006-05-16 06:10:58 +00001123 // Finally, emit the code for all of the scheduled instructions.
Roman Levenstein9cac5252008-04-16 16:15:27 +00001124 DenseMap<SDOperand, unsigned> VRBaseMap;
Evan Cheng42d60272007-09-26 21:36:17 +00001125 DenseMap<SUnit*, unsigned> CopyVRBaseMap;
Evan Chenge165a782006-05-11 23:55:42 +00001126 for (unsigned i = 0, e = Sequence.size(); i != e; i++) {
Evan Cheng8a50f1f2008-04-03 16:36:07 +00001127 SUnit *SU = Sequence[i];
1128 if (!SU) {
Evan Chenge165a782006-05-11 23:55:42 +00001129 // Null SUnit* is a noop.
1130 EmitNoop();
Evan Cheng8a50f1f2008-04-03 16:36:07 +00001131 continue;
Evan Chenge165a782006-05-11 23:55:42 +00001132 }
Evan Cheng8a50f1f2008-04-03 16:36:07 +00001133 for (unsigned j = 0, ee = SU->FlaggedNodes.size(); j != ee; ++j)
Dan Gohman4c8c8302008-06-21 15:52:51 +00001134 EmitNode(SU->FlaggedNodes[j], SU->OrigNode != SU, VRBaseMap);
Evan Cheng8a50f1f2008-04-03 16:36:07 +00001135 if (!SU->Node)
1136 EmitCrossRCCopy(SU, CopyVRBaseMap);
1137 else
Dan Gohman4c8c8302008-06-21 15:52:51 +00001138 EmitNode(SU->Node, SU->OrigNode != SU, VRBaseMap);
Evan Chenge165a782006-05-11 23:55:42 +00001139 }
Evan Cheng9e233362008-03-12 22:19:41 +00001140
1141 if (isEntryBB && SchedLiveInCopies)
1142 EmitLiveInCopies(MF->begin());
Evan Chenge165a782006-05-11 23:55:42 +00001143}
1144
1145/// dump - dump the schedule.
1146void ScheduleDAG::dumpSchedule() const {
1147 for (unsigned i = 0, e = Sequence.size(); i != e; i++) {
1148 if (SUnit *SU = Sequence[i])
1149 SU->dump(&DAG);
1150 else
Bill Wendling832171c2006-12-07 20:04:42 +00001151 cerr << "**** NOOP ****\n";
Evan Chenge165a782006-05-11 23:55:42 +00001152 }
1153}
1154
1155
Evan Chenga9c20912006-01-21 02:32:06 +00001156/// Run - perform scheduling.
1157///
1158MachineBasicBlock *ScheduleDAG::Run() {
Evan Chenga9c20912006-01-21 02:32:06 +00001159 Schedule();
1160 return BB;
Chris Lattnerd32b2362005-08-18 18:45:24 +00001161}
Evan Cheng4ef10862006-01-23 07:01:07 +00001162
Evan Chenge165a782006-05-11 23:55:42 +00001163/// SUnit - Scheduling unit. It's an wrapper around either a single SDNode or
1164/// a group of nodes flagged together.
1165void SUnit::dump(const SelectionDAG *G) const {
Bill Wendling832171c2006-12-07 20:04:42 +00001166 cerr << "SU(" << NodeNum << "): ";
Evan Cheng42d60272007-09-26 21:36:17 +00001167 if (Node)
1168 Node->dump(G);
1169 else
1170 cerr << "CROSS RC COPY ";
Bill Wendling832171c2006-12-07 20:04:42 +00001171 cerr << "\n";
Evan Chenge165a782006-05-11 23:55:42 +00001172 if (FlaggedNodes.size() != 0) {
1173 for (unsigned i = 0, e = FlaggedNodes.size(); i != e; i++) {
Bill Wendling832171c2006-12-07 20:04:42 +00001174 cerr << " ";
Evan Chenge165a782006-05-11 23:55:42 +00001175 FlaggedNodes[i]->dump(G);
Bill Wendling832171c2006-12-07 20:04:42 +00001176 cerr << "\n";
Evan Chenge165a782006-05-11 23:55:42 +00001177 }
1178 }
1179}
Evan Cheng4ef10862006-01-23 07:01:07 +00001180
Evan Chenge165a782006-05-11 23:55:42 +00001181void SUnit::dumpAll(const SelectionDAG *G) const {
1182 dump(G);
1183
Bill Wendling832171c2006-12-07 20:04:42 +00001184 cerr << " # preds left : " << NumPredsLeft << "\n";
1185 cerr << " # succs left : " << NumSuccsLeft << "\n";
Bill Wendling832171c2006-12-07 20:04:42 +00001186 cerr << " Latency : " << Latency << "\n";
1187 cerr << " Depth : " << Depth << "\n";
1188 cerr << " Height : " << Height << "\n";
Evan Chenge165a782006-05-11 23:55:42 +00001189
1190 if (Preds.size() != 0) {
Bill Wendling832171c2006-12-07 20:04:42 +00001191 cerr << " Predecessors:\n";
Chris Lattner228a18e2006-08-17 00:09:56 +00001192 for (SUnit::const_succ_iterator I = Preds.begin(), E = Preds.end();
1193 I != E; ++I) {
Evan Cheng713a98d2007-09-19 01:38:40 +00001194 if (I->isCtrl)
Bill Wendling832171c2006-12-07 20:04:42 +00001195 cerr << " ch #";
Evan Chenge165a782006-05-11 23:55:42 +00001196 else
Bill Wendling832171c2006-12-07 20:04:42 +00001197 cerr << " val #";
Evan Chenga6fb1b62007-09-25 01:54:36 +00001198 cerr << I->Dep << " - SU(" << I->Dep->NodeNum << ")";
1199 if (I->isSpecial)
1200 cerr << " *";
1201 cerr << "\n";
Evan Chenge165a782006-05-11 23:55:42 +00001202 }
1203 }
1204 if (Succs.size() != 0) {
Bill Wendling832171c2006-12-07 20:04:42 +00001205 cerr << " Successors:\n";
Chris Lattner228a18e2006-08-17 00:09:56 +00001206 for (SUnit::const_succ_iterator I = Succs.begin(), E = Succs.end();
1207 I != E; ++I) {
Evan Cheng713a98d2007-09-19 01:38:40 +00001208 if (I->isCtrl)
Bill Wendling832171c2006-12-07 20:04:42 +00001209 cerr << " ch #";
Evan Chenge165a782006-05-11 23:55:42 +00001210 else
Bill Wendling832171c2006-12-07 20:04:42 +00001211 cerr << " val #";
Evan Chenga6fb1b62007-09-25 01:54:36 +00001212 cerr << I->Dep << " - SU(" << I->Dep->NodeNum << ")";
1213 if (I->isSpecial)
1214 cerr << " *";
1215 cerr << "\n";
Evan Chenge165a782006-05-11 23:55:42 +00001216 }
1217 }
Bill Wendling832171c2006-12-07 20:04:42 +00001218 cerr << "\n";
Evan Chenge165a782006-05-11 23:55:42 +00001219}