blob: d61a0982c32ea25081619270c7a73f96a7d3f74c [file] [log] [blame]
Evan Chenga9c20912006-01-21 02:32:06 +00001//===---- ScheduleDAG.cpp - Implement the ScheduleDAG class ---------------===//
Chris Lattnerd32b2362005-08-18 18:45:24 +00002//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner4ee451d2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Chris Lattnerd32b2362005-08-18 18:45:24 +00007//
8//===----------------------------------------------------------------------===//
9//
Jim Laskeye6b90fb2005-09-26 21:57:04 +000010// This implements a simple two pass scheduler. The first pass attempts to push
11// backward any lengthy instructions and critical paths. The second pass packs
12// instructions into semi-optimal time slots.
Chris Lattnerd32b2362005-08-18 18:45:24 +000013//
14//===----------------------------------------------------------------------===//
15
Dale Johannesene7e7d0d2007-07-13 17:13:54 +000016#define DEBUG_TYPE "pre-RA-sched"
Nate Begemane1795842008-02-14 08:57:00 +000017#include "llvm/Constants.h"
Reid Spencere5530da2007-01-12 23:31:12 +000018#include "llvm/Type.h"
Chris Lattnerb0d21ef2006-03-08 04:25:59 +000019#include "llvm/CodeGen/ScheduleDAG.h"
Chris Lattner5839bf22005-08-26 17:15:30 +000020#include "llvm/CodeGen/MachineConstantPool.h"
Chris Lattner4ccd4062005-08-19 20:45:43 +000021#include "llvm/CodeGen/MachineFunction.h"
Evan Cheng8a50f1f2008-04-03 16:36:07 +000022#include "llvm/CodeGen/MachineInstrBuilder.h"
Chris Lattner84bc5422007-12-31 04:13:23 +000023#include "llvm/CodeGen/MachineRegisterInfo.h"
Owen Anderson07000c62006-05-12 06:33:49 +000024#include "llvm/Target/TargetData.h"
Chris Lattner2d973e42005-08-18 20:07:59 +000025#include "llvm/Target/TargetMachine.h"
26#include "llvm/Target/TargetInstrInfo.h"
Chris Lattner025c39b2005-08-26 20:54:47 +000027#include "llvm/Target/TargetLowering.h"
Evan Cheng643afa52008-02-28 07:40:24 +000028#include "llvm/ADT/Statistic.h"
Evan Cheng9e233362008-03-12 22:19:41 +000029#include "llvm/Support/CommandLine.h"
Evan Chenge165a782006-05-11 23:55:42 +000030#include "llvm/Support/Debug.h"
Chris Lattner54a30b92006-03-20 01:51:46 +000031#include "llvm/Support/MathExtras.h"
Chris Lattnerd32b2362005-08-18 18:45:24 +000032using namespace llvm;
33
Evan Cheng643afa52008-02-28 07:40:24 +000034STATISTIC(NumCommutes, "Number of instructions commuted");
35
Evan Cheng9e233362008-03-12 22:19:41 +000036namespace {
37 static cl::opt<bool>
38 SchedLiveInCopies("schedule-livein-copies",
39 cl::desc("Schedule copies of livein registers"),
40 cl::init(false));
41}
42
Chris Lattner84bc5422007-12-31 04:13:23 +000043ScheduleDAG::ScheduleDAG(SelectionDAG &dag, MachineBasicBlock *bb,
44 const TargetMachine &tm)
Evan Cheng9e233362008-03-12 22:19:41 +000045 : DAG(dag), BB(bb), TM(tm), MRI(BB->getParent()->getRegInfo()) {
Evan Cheng8a50f1f2008-04-03 16:36:07 +000046 TII = TM.getInstrInfo();
47 MF = &DAG.getMachineFunction();
48 TRI = TM.getRegisterInfo();
49 TLI = &DAG.getTargetLoweringInfo();
50 ConstPool = BB->getParent()->getConstantPool();
Chris Lattner84bc5422007-12-31 04:13:23 +000051}
Evan Chenga6fb1b62007-09-25 01:54:36 +000052
Evan Chenga6fb1b62007-09-25 01:54:36 +000053/// CheckForPhysRegDependency - Check if the dependency between def and use of
54/// a specified operand is a physical register dependency. If so, returns the
55/// register and the cost of copying the register.
56static void CheckForPhysRegDependency(SDNode *Def, SDNode *Use, unsigned Op,
Dan Gohman6f0d0242008-02-10 18:45:23 +000057 const TargetRegisterInfo *TRI,
Evan Chenga6fb1b62007-09-25 01:54:36 +000058 const TargetInstrInfo *TII,
59 unsigned &PhysReg, int &Cost) {
60 if (Op != 2 || Use->getOpcode() != ISD::CopyToReg)
61 return;
62
63 unsigned Reg = cast<RegisterSDNode>(Use->getOperand(1))->getReg();
Dan Gohman6f0d0242008-02-10 18:45:23 +000064 if (TargetRegisterInfo::isVirtualRegister(Reg))
Evan Chenga6fb1b62007-09-25 01:54:36 +000065 return;
66
67 unsigned ResNo = Use->getOperand(2).ResNo;
68 if (Def->isTargetOpcode()) {
Chris Lattner749c6f62008-01-07 07:27:27 +000069 const TargetInstrDesc &II = TII->get(Def->getTargetOpcode());
Chris Lattner349c4952008-01-07 03:13:06 +000070 if (ResNo >= II.getNumDefs() &&
71 II.ImplicitDefs[ResNo - II.getNumDefs()] == Reg) {
Evan Chenga6fb1b62007-09-25 01:54:36 +000072 PhysReg = Reg;
73 const TargetRegisterClass *RC =
Evan Cheng676dd7c2008-03-11 07:19:34 +000074 TRI->getPhysicalRegisterRegClass(Reg, Def->getValueType(ResNo));
Evan Chenga6fb1b62007-09-25 01:54:36 +000075 Cost = RC->getCopyCost();
76 }
77 }
78}
79
80SUnit *ScheduleDAG::Clone(SUnit *Old) {
81 SUnit *SU = NewSUnit(Old->Node);
Dan Gohman4c8c8302008-06-21 15:52:51 +000082 SU->OrigNode = Old->OrigNode;
Dan Gohman45f36ea2008-03-10 23:48:14 +000083 SU->FlaggedNodes = Old->FlaggedNodes;
Evan Chenga6fb1b62007-09-25 01:54:36 +000084 SU->Latency = Old->Latency;
85 SU->isTwoAddress = Old->isTwoAddress;
86 SU->isCommutable = Old->isCommutable;
Evan Cheng22a52992007-09-28 22:32:30 +000087 SU->hasPhysRegDefs = Old->hasPhysRegDefs;
Evan Chenga6fb1b62007-09-25 01:54:36 +000088 return SU;
89}
90
Evan Chengf10c9732007-10-05 01:39:18 +000091
Evan Chenge165a782006-05-11 23:55:42 +000092/// BuildSchedUnits - Build SUnits from the selection dag that we are input.
93/// This SUnit graph is similar to the SelectionDAG, but represents flagged
94/// together nodes with a single SUnit.
95void ScheduleDAG::BuildSchedUnits() {
96 // Reserve entries in the vector for each of the SUnits we are creating. This
97 // ensure that reallocation of the vector won't happen, so SUnit*'s won't get
98 // invalidated.
Dan Gohman3461cc92008-06-20 17:15:19 +000099 SUnits.reserve(DAG.allnodes_size());
Evan Chenge165a782006-05-11 23:55:42 +0000100
Evan Chenge165a782006-05-11 23:55:42 +0000101 for (SelectionDAG::allnodes_iterator NI = DAG.allnodes_begin(),
102 E = DAG.allnodes_end(); NI != E; ++NI) {
103 if (isPassiveNode(NI)) // Leaf node, e.g. a TargetImmediate.
104 continue;
105
106 // If this node has already been processed, stop now.
Dan Gohman4c8c8302008-06-21 15:52:51 +0000107 if (SUnitMap.count(NI)) continue;
Evan Chenge165a782006-05-11 23:55:42 +0000108
109 SUnit *NodeSUnit = NewSUnit(NI);
110
111 // See if anything is flagged to this node, if so, add them to flagged
112 // nodes. Nodes can have at most one flag input and one flag output. Flags
113 // are required the be the last operand and result of a node.
114
115 // Scan up, adding flagged preds to FlaggedNodes.
116 SDNode *N = NI;
Evan Cheng3b97acd2006-08-07 22:12:12 +0000117 if (N->getNumOperands() &&
118 N->getOperand(N->getNumOperands()-1).getValueType() == MVT::Flag) {
119 do {
120 N = N->getOperand(N->getNumOperands()-1).Val;
121 NodeSUnit->FlaggedNodes.push_back(N);
Dan Gohman4c8c8302008-06-21 15:52:51 +0000122 bool isNew = SUnitMap.insert(std::make_pair(N, NodeSUnit));
123 isNew = isNew;
124 assert(isNew && "Node already inserted!");
Evan Cheng3b97acd2006-08-07 22:12:12 +0000125 } while (N->getNumOperands() &&
126 N->getOperand(N->getNumOperands()-1).getValueType()== MVT::Flag);
127 std::reverse(NodeSUnit->FlaggedNodes.begin(),
128 NodeSUnit->FlaggedNodes.end());
Evan Chenge165a782006-05-11 23:55:42 +0000129 }
130
131 // Scan down, adding this node and any flagged succs to FlaggedNodes if they
132 // have a user of the flag operand.
133 N = NI;
134 while (N->getValueType(N->getNumValues()-1) == MVT::Flag) {
135 SDOperand FlagVal(N, N->getNumValues()-1);
136
137 // There are either zero or one users of the Flag result.
138 bool HasFlagUse = false;
139 for (SDNode::use_iterator UI = N->use_begin(), E = N->use_end();
140 UI != E; ++UI)
Roman Levensteindc1adac2008-04-07 10:06:32 +0000141 if (FlagVal.isOperandOf(UI->getUser())) {
Evan Chenge165a782006-05-11 23:55:42 +0000142 HasFlagUse = true;
143 NodeSUnit->FlaggedNodes.push_back(N);
Dan Gohman4c8c8302008-06-21 15:52:51 +0000144 bool isNew = SUnitMap.insert(std::make_pair(N, NodeSUnit));
145 isNew = isNew;
146 assert(isNew && "Node already inserted!");
Roman Levensteindc1adac2008-04-07 10:06:32 +0000147 N = UI->getUser();
Evan Chenge165a782006-05-11 23:55:42 +0000148 break;
149 }
Chris Lattner228a18e2006-08-17 00:09:56 +0000150 if (!HasFlagUse) break;
Evan Chenge165a782006-05-11 23:55:42 +0000151 }
152
153 // Now all flagged nodes are in FlaggedNodes and N is the bottom-most node.
154 // Update the SUnit
155 NodeSUnit->Node = N;
Dan Gohman4c8c8302008-06-21 15:52:51 +0000156 bool isNew = SUnitMap.insert(std::make_pair(N, NodeSUnit));
157 isNew = isNew;
158 assert(isNew && "Node already inserted!");
Evan Chengf10c9732007-10-05 01:39:18 +0000159
160 ComputeLatency(NodeSUnit);
Evan Chenge165a782006-05-11 23:55:42 +0000161 }
162
163 // Pass 2: add the preds, succs, etc.
164 for (unsigned su = 0, e = SUnits.size(); su != e; ++su) {
165 SUnit *SU = &SUnits[su];
166 SDNode *MainNode = SU->Node;
167
168 if (MainNode->isTargetOpcode()) {
169 unsigned Opc = MainNode->getTargetOpcode();
Chris Lattner749c6f62008-01-07 07:27:27 +0000170 const TargetInstrDesc &TID = TII->get(Opc);
Chris Lattner349c4952008-01-07 03:13:06 +0000171 for (unsigned i = 0; i != TID.getNumOperands(); ++i) {
Evan Chenga6fb1b62007-09-25 01:54:36 +0000172 if (TID.getOperandConstraint(i, TOI::TIED_TO) != -1) {
Evan Cheng95f6ede2006-11-04 09:44:31 +0000173 SU->isTwoAddress = true;
174 break;
175 }
176 }
Chris Lattner0ff23962008-01-07 06:42:05 +0000177 if (TID.isCommutable())
Evan Cheng13d41b92006-05-12 01:58:24 +0000178 SU->isCommutable = true;
Evan Chenge165a782006-05-11 23:55:42 +0000179 }
180
181 // Find all predecessors and successors of the group.
182 // Temporarily add N to make code simpler.
183 SU->FlaggedNodes.push_back(MainNode);
184
185 for (unsigned n = 0, e = SU->FlaggedNodes.size(); n != e; ++n) {
186 SDNode *N = SU->FlaggedNodes[n];
Evan Cheng22a52992007-09-28 22:32:30 +0000187 if (N->isTargetOpcode() &&
Chris Lattner349c4952008-01-07 03:13:06 +0000188 TII->get(N->getTargetOpcode()).getImplicitDefs() &&
189 CountResults(N) > TII->get(N->getTargetOpcode()).getNumDefs())
Evan Cheng22a52992007-09-28 22:32:30 +0000190 SU->hasPhysRegDefs = true;
Evan Chenge165a782006-05-11 23:55:42 +0000191
192 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
193 SDNode *OpN = N->getOperand(i).Val;
194 if (isPassiveNode(OpN)) continue; // Not scheduled.
Dan Gohman4c8c8302008-06-21 15:52:51 +0000195 SUnit *OpSU = SUnitMap[OpN];
Evan Chenge165a782006-05-11 23:55:42 +0000196 assert(OpSU && "Node has no SUnit!");
197 if (OpSU == SU) continue; // In the same group.
198
Duncan Sands83ec4b62008-06-06 12:08:01 +0000199 MVT OpVT = N->getOperand(i).getValueType();
Evan Chenge165a782006-05-11 23:55:42 +0000200 assert(OpVT != MVT::Flag && "Flagged nodes should be in same sunit!");
201 bool isChain = OpVT == MVT::Other;
Evan Chenga6fb1b62007-09-25 01:54:36 +0000202
203 unsigned PhysReg = 0;
204 int Cost = 1;
205 // Determine if this is a physical register dependency.
Dan Gohman6f0d0242008-02-10 18:45:23 +0000206 CheckForPhysRegDependency(OpN, N, i, TRI, TII, PhysReg, Cost);
Evan Chenga6fb1b62007-09-25 01:54:36 +0000207 SU->addPred(OpSU, isChain, false, PhysReg, Cost);
Evan Chenge165a782006-05-11 23:55:42 +0000208 }
209 }
210
211 // Remove MainNode from FlaggedNodes again.
212 SU->FlaggedNodes.pop_back();
213 }
214
215 return;
216}
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;
226 } else {
227 SU->Latency = 0;
228 if (SU->Node->isTargetOpcode()) {
Chris Lattnerba6da5d2008-01-07 02:46:03 +0000229 unsigned SchedClass =
230 TII->get(SU->Node->getTargetOpcode()).getSchedClass();
Dan Gohmancfbb2f02008-03-25 21:45:14 +0000231 const InstrStage *S = InstrItins.begin(SchedClass);
232 const InstrStage *E = InstrItins.end(SchedClass);
Evan Chengf10c9732007-10-05 01:39:18 +0000233 for (; S != E; ++S)
234 SU->Latency += S->Cycles;
235 }
236 for (unsigned i = 0, e = SU->FlaggedNodes.size(); i != e; ++i) {
237 SDNode *FNode = SU->FlaggedNodes[i];
238 if (FNode->isTargetOpcode()) {
Chris Lattnerba6da5d2008-01-07 02:46:03 +0000239 unsigned SchedClass =TII->get(FNode->getTargetOpcode()).getSchedClass();
Dan Gohmancfbb2f02008-03-25 21:45:14 +0000240 const InstrStage *S = InstrItins.begin(SchedClass);
241 const InstrStage *E = InstrItins.end(SchedClass);
Evan Chengf10c9732007-10-05 01:39:18 +0000242 for (; S != E; ++S)
243 SU->Latency += S->Cycles;
244 }
245 }
246 }
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
Jim Laskey60f09922006-07-21 20:57:35 +0000392static const TargetRegisterClass *getInstrOperandRegClass(
Dan Gohman6f0d0242008-02-10 18:45:23 +0000393 const TargetRegisterInfo *TRI,
Jim Laskey60f09922006-07-21 20:57:35 +0000394 const TargetInstrInfo *TII,
Chris Lattner749c6f62008-01-07 07:27:27 +0000395 const TargetInstrDesc &II,
Jim Laskey60f09922006-07-21 20:57:35 +0000396 unsigned Op) {
Chris Lattner749c6f62008-01-07 07:27:27 +0000397 if (Op >= II.getNumOperands()) {
398 assert(II.isVariadic() && "Invalid operand # of instruction");
Jim Laskey60f09922006-07-21 20:57:35 +0000399 return NULL;
400 }
Chris Lattner749c6f62008-01-07 07:27:27 +0000401 if (II.OpInfo[Op].isLookupPtrRegClass())
Chris Lattner8ca5c672008-01-07 02:39:19 +0000402 return TII->getPointerRegClass();
Dan Gohman6f0d0242008-02-10 18:45:23 +0000403 return TRI->getRegClass(II.OpInfo[Op].RegClass);
Jim Laskey60f09922006-07-21 20:57:35 +0000404}
405
Evan Chenga6fb1b62007-09-25 01:54:36 +0000406void ScheduleDAG::EmitCopyFromReg(SDNode *Node, unsigned ResNo,
Dan Gohman4c8c8302008-06-21 15:52:51 +0000407 bool IsClone, unsigned SrcReg,
Roman Levenstein9cac5252008-04-16 16:15:27 +0000408 DenseMap<SDOperand, unsigned> &VRBaseMap) {
Evan Cheng84097472007-08-02 00:28:15 +0000409 unsigned VRBase = 0;
Dan Gohman6f0d0242008-02-10 18:45:23 +0000410 if (TargetRegisterInfo::isVirtualRegister(SrcReg)) {
Evan Cheng84097472007-08-02 00:28:15 +0000411 // Just use the input register directly!
Dan Gohman4c8c8302008-06-21 15:52:51 +0000412 if (IsClone)
Evan Chenga6fb1b62007-09-25 01:54:36 +0000413 VRBaseMap.erase(SDOperand(Node, ResNo));
Evan Cheng84097472007-08-02 00:28:15 +0000414 bool isNew = VRBaseMap.insert(std::make_pair(SDOperand(Node,ResNo),SrcReg));
Evan Cheng97e60d92008-05-14 21:08:07 +0000415 isNew = isNew; // Silence compiler warning.
Evan Cheng84097472007-08-02 00:28:15 +0000416 assert(isNew && "Node emitted out of order - early");
417 return;
418 }
419
420 // If the node is only used by a CopyToReg and the dest reg is a vreg, use
421 // the CopyToReg'd destination register instead of creating a new vreg.
Evan Chenga6fb1b62007-09-25 01:54:36 +0000422 bool MatchReg = true;
Evan Cheng84097472007-08-02 00:28:15 +0000423 for (SDNode::use_iterator UI = Node->use_begin(), E = Node->use_end();
424 UI != E; ++UI) {
Roman Levensteindc1adac2008-04-07 10:06:32 +0000425 SDNode *Use = UI->getUser();
Evan Chenga6fb1b62007-09-25 01:54:36 +0000426 bool Match = true;
Evan Cheng84097472007-08-02 00:28:15 +0000427 if (Use->getOpcode() == ISD::CopyToReg &&
428 Use->getOperand(2).Val == Node &&
429 Use->getOperand(2).ResNo == ResNo) {
430 unsigned DestReg = cast<RegisterSDNode>(Use->getOperand(1))->getReg();
Dan Gohman6f0d0242008-02-10 18:45:23 +0000431 if (TargetRegisterInfo::isVirtualRegister(DestReg)) {
Evan Cheng84097472007-08-02 00:28:15 +0000432 VRBase = DestReg;
Evan Chenga6fb1b62007-09-25 01:54:36 +0000433 Match = false;
434 } else if (DestReg != SrcReg)
435 Match = false;
436 } else {
437 for (unsigned i = 0, e = Use->getNumOperands(); i != e; ++i) {
438 SDOperand Op = Use->getOperand(i);
Evan Cheng7c07aeb2007-12-14 08:25:15 +0000439 if (Op.Val != Node || Op.ResNo != ResNo)
Evan Chenga6fb1b62007-09-25 01:54:36 +0000440 continue;
Duncan Sands83ec4b62008-06-06 12:08:01 +0000441 MVT VT = Node->getValueType(Op.ResNo);
Evan Chenga6fb1b62007-09-25 01:54:36 +0000442 if (VT != MVT::Other && VT != MVT::Flag)
443 Match = false;
Evan Cheng84097472007-08-02 00:28:15 +0000444 }
445 }
Evan Chenga6fb1b62007-09-25 01:54:36 +0000446 MatchReg &= Match;
447 if (VRBase)
448 break;
Evan Cheng84097472007-08-02 00:28:15 +0000449 }
450
Chris Lattner02b6d252008-03-09 08:49:15 +0000451 const TargetRegisterClass *SrcRC = 0, *DstRC = 0;
Evan Cheng676dd7c2008-03-11 07:19:34 +0000452 SrcRC = TRI->getPhysicalRegisterRegClass(SrcReg, Node->getValueType(ResNo));
Chris Lattner02b6d252008-03-09 08:49:15 +0000453
Evan Chenga6fb1b62007-09-25 01:54:36 +0000454 // Figure out the register class to create for the destreg.
Chris Lattner02b6d252008-03-09 08:49:15 +0000455 if (VRBase) {
Evan Cheng9e233362008-03-12 22:19:41 +0000456 DstRC = MRI.getRegClass(VRBase);
Chris Lattner02b6d252008-03-09 08:49:15 +0000457 } else {
Evan Cheng8a50f1f2008-04-03 16:36:07 +0000458 DstRC = TLI->getRegClassFor(Node->getValueType(ResNo));
Chris Lattner02b6d252008-03-09 08:49:15 +0000459 }
Evan Chenga6fb1b62007-09-25 01:54:36 +0000460
461 // If all uses are reading from the src physical register and copying the
462 // register is either impossible or very expensive, then don't create a copy.
Chris Lattner02b6d252008-03-09 08:49:15 +0000463 if (MatchReg && SrcRC->getCopyCost() < 0) {
Evan Chenga6fb1b62007-09-25 01:54:36 +0000464 VRBase = SrcReg;
465 } else {
Evan Cheng84097472007-08-02 00:28:15 +0000466 // Create the reg, emit the copy.
Evan Cheng9e233362008-03-12 22:19:41 +0000467 VRBase = MRI.createVirtualRegister(DstRC);
Chris Lattner02b6d252008-03-09 08:49:15 +0000468 TII->copyRegToReg(*BB, BB->end(), VRBase, SrcReg, DstRC, SrcRC);
Evan Cheng84097472007-08-02 00:28:15 +0000469 }
Evan Cheng84097472007-08-02 00:28:15 +0000470
Dan Gohman4c8c8302008-06-21 15:52:51 +0000471 if (IsClone)
Evan Chenga6fb1b62007-09-25 01:54:36 +0000472 VRBaseMap.erase(SDOperand(Node, ResNo));
Evan Cheng84097472007-08-02 00:28:15 +0000473 bool isNew = VRBaseMap.insert(std::make_pair(SDOperand(Node,ResNo), VRBase));
Evan Cheng97e60d92008-05-14 21:08:07 +0000474 isNew = isNew; // Silence compiler warning.
Evan Cheng84097472007-08-02 00:28:15 +0000475 assert(isNew && "Node emitted out of order - early");
476}
477
Evan Cheng8a50f1f2008-04-03 16:36:07 +0000478/// getDstOfCopyToRegUse - If the only use of the specified result number of
479/// node is a CopyToReg, return its destination register. Return 0 otherwise.
480unsigned ScheduleDAG::getDstOfOnlyCopyToRegUse(SDNode *Node,
481 unsigned ResNo) const {
482 if (!Node->hasOneUse())
483 return 0;
484
Roman Levensteindc1adac2008-04-07 10:06:32 +0000485 SDNode *Use = Node->use_begin()->getUser();
Evan Cheng8a50f1f2008-04-03 16:36:07 +0000486 if (Use->getOpcode() == ISD::CopyToReg &&
487 Use->getOperand(2).Val == Node &&
488 Use->getOperand(2).ResNo == ResNo) {
489 unsigned Reg = cast<RegisterSDNode>(Use->getOperand(1))->getReg();
490 if (TargetRegisterInfo::isVirtualRegister(Reg))
491 return Reg;
492 }
493 return 0;
494}
495
Evan Chengda47e6e2008-03-15 00:03:38 +0000496void ScheduleDAG::CreateVirtualRegisters(SDNode *Node, MachineInstr *MI,
Evan Cheng8a50f1f2008-04-03 16:36:07 +0000497 const TargetInstrDesc &II,
Roman Levenstein9cac5252008-04-16 16:15:27 +0000498 DenseMap<SDOperand, unsigned> &VRBaseMap) {
Evan Cheng8a50f1f2008-04-03 16:36:07 +0000499 assert(Node->getTargetOpcode() != TargetInstrInfo::IMPLICIT_DEF &&
500 "IMPLICIT_DEF should have been handled as a special case elsewhere!");
501
Chris Lattner349c4952008-01-07 03:13:06 +0000502 for (unsigned i = 0; i < II.getNumDefs(); ++i) {
Evan Chengaf825c82007-07-10 07:08:32 +0000503 // If the specific node value is only used by a CopyToReg and the dest reg
504 // is a vreg, use the CopyToReg'd destination register instead of creating
505 // a new vreg.
506 unsigned VRBase = 0;
507 for (SDNode::use_iterator UI = Node->use_begin(), E = Node->use_end();
508 UI != E; ++UI) {
Roman Levensteindc1adac2008-04-07 10:06:32 +0000509 SDNode *Use = UI->getUser();
Evan Chengaf825c82007-07-10 07:08:32 +0000510 if (Use->getOpcode() == ISD::CopyToReg &&
511 Use->getOperand(2).Val == Node &&
512 Use->getOperand(2).ResNo == i) {
513 unsigned Reg = cast<RegisterSDNode>(Use->getOperand(1))->getReg();
Dan Gohman6f0d0242008-02-10 18:45:23 +0000514 if (TargetRegisterInfo::isVirtualRegister(Reg)) {
Evan Chengaf825c82007-07-10 07:08:32 +0000515 VRBase = Reg;
Chris Lattner8019f412007-12-30 00:41:17 +0000516 MI->addOperand(MachineOperand::CreateReg(Reg, true));
Evan Chengaf825c82007-07-10 07:08:32 +0000517 break;
518 }
519 }
520 }
521
Evan Cheng84097472007-08-02 00:28:15 +0000522 // Create the result registers for this node and add the result regs to
523 // the machine instruction.
Evan Chengaf825c82007-07-10 07:08:32 +0000524 if (VRBase == 0) {
Evan Cheng8a50f1f2008-04-03 16:36:07 +0000525 const TargetRegisterClass *RC = getInstrOperandRegClass(TRI, TII, II, i);
Evan Chengaf825c82007-07-10 07:08:32 +0000526 assert(RC && "Isn't a register operand!");
Evan Cheng9e233362008-03-12 22:19:41 +0000527 VRBase = MRI.createVirtualRegister(RC);
Chris Lattner8019f412007-12-30 00:41:17 +0000528 MI->addOperand(MachineOperand::CreateReg(VRBase, true));
Evan Chengaf825c82007-07-10 07:08:32 +0000529 }
530
531 bool isNew = VRBaseMap.insert(std::make_pair(SDOperand(Node,i), VRBase));
Evan Cheng97e60d92008-05-14 21:08:07 +0000532 isNew = isNew; // Silence compiler warning.
Evan Chengaf825c82007-07-10 07:08:32 +0000533 assert(isNew && "Node emitted out of order - early");
Jim Laskeye6b90fb2005-09-26 21:57:04 +0000534 }
Jim Laskeye6b90fb2005-09-26 21:57:04 +0000535}
536
Chris Lattnerdf375062006-03-10 07:25:12 +0000537/// getVR - Return the virtual register corresponding to the specified result
538/// of the specified node.
Evan Cheng8a50f1f2008-04-03 16:36:07 +0000539unsigned ScheduleDAG::getVR(SDOperand Op,
Roman Levenstein9cac5252008-04-16 16:15:27 +0000540 DenseMap<SDOperand, unsigned> &VRBaseMap) {
Evan Cheng8a50f1f2008-04-03 16:36:07 +0000541 if (Op.isTargetOpcode() &&
542 Op.getTargetOpcode() == TargetInstrInfo::IMPLICIT_DEF) {
543 // Add an IMPLICIT_DEF instruction before every use.
544 unsigned VReg = getDstOfOnlyCopyToRegUse(Op.Val, Op.ResNo);
545 // IMPLICIT_DEF can produce any type of result so its TargetInstrDesc
546 // does not include operand register class info.
547 if (!VReg) {
548 const TargetRegisterClass *RC = TLI->getRegClassFor(Op.getValueType());
549 VReg = MRI.createVirtualRegister(RC);
550 }
551 BuildMI(BB, TII->get(TargetInstrInfo::IMPLICIT_DEF), VReg);
552 return VReg;
553 }
554
Roman Levenstein9cac5252008-04-16 16:15:27 +0000555 DenseMap<SDOperand, unsigned>::iterator I = VRBaseMap.find(Op);
Chris Lattnerdf375062006-03-10 07:25:12 +0000556 assert(I != VRBaseMap.end() && "Node emitted out of order - late");
Evan Chengaf825c82007-07-10 07:08:32 +0000557 return I->second;
Chris Lattnerdf375062006-03-10 07:25:12 +0000558}
559
560
Chris Lattnered18b682006-02-24 18:54:03 +0000561/// AddOperand - Add the specified operand to the specified machine instr. II
562/// specifies the instruction information for the node, and IIOpNum is the
563/// operand number (in the II) that we are adding. IIOpNum and II are used for
564/// assertions only.
565void ScheduleDAG::AddOperand(MachineInstr *MI, SDOperand Op,
566 unsigned IIOpNum,
Chris Lattner749c6f62008-01-07 07:27:27 +0000567 const TargetInstrDesc *II,
Roman Levenstein9cac5252008-04-16 16:15:27 +0000568 DenseMap<SDOperand, unsigned> &VRBaseMap) {
Chris Lattnered18b682006-02-24 18:54:03 +0000569 if (Op.isTargetOpcode()) {
570 // Note that this case is redundant with the final else block, but we
571 // include it because it is the most common and it makes the logic
572 // simpler here.
573 assert(Op.getValueType() != MVT::Other &&
574 Op.getValueType() != MVT::Flag &&
575 "Chain and flag operands should occur at end of operand list!");
Chris Lattnered18b682006-02-24 18:54:03 +0000576 // Get/emit the operand.
Chris Lattnerdf375062006-03-10 07:25:12 +0000577 unsigned VReg = getVR(Op, VRBaseMap);
Chris Lattner749c6f62008-01-07 07:27:27 +0000578 const TargetInstrDesc &TID = MI->getDesc();
Evan Cheng8a50f1f2008-04-03 16:36:07 +0000579 bool isOptDef = IIOpNum < TID.getNumOperands() &&
580 TID.OpInfo[IIOpNum].isOptionalDef();
Chris Lattner8019f412007-12-30 00:41:17 +0000581 MI->addOperand(MachineOperand::CreateReg(VReg, isOptDef));
Chris Lattnered18b682006-02-24 18:54:03 +0000582
583 // Verify that it is right.
Dan Gohman6f0d0242008-02-10 18:45:23 +0000584 assert(TargetRegisterInfo::isVirtualRegister(VReg) && "Not a vreg?");
Chris Lattnerb7795802008-03-11 00:59:28 +0000585#ifndef NDEBUG
Chris Lattnered18b682006-02-24 18:54:03 +0000586 if (II) {
Chris Lattnerb7795802008-03-11 00:59:28 +0000587 // There may be no register class for this operand if it is a variadic
588 // argument (RC will be NULL in this case). In this case, we just assume
589 // the regclass is ok.
Jim Laskey60f09922006-07-21 20:57:35 +0000590 const TargetRegisterClass *RC =
Dan Gohman6f0d0242008-02-10 18:45:23 +0000591 getInstrOperandRegClass(TRI, TII, *II, IIOpNum);
Chris Lattnerc5733ac2008-03-11 03:14:42 +0000592 assert((RC || II->isVariadic()) && "Expected reg class info!");
Evan Cheng9e233362008-03-12 22:19:41 +0000593 const TargetRegisterClass *VRC = MRI.getRegClass(VReg);
Chris Lattnerb7795802008-03-11 00:59:28 +0000594 if (RC && VRC != RC) {
Chris Lattner01528292007-02-15 18:17:56 +0000595 cerr << "Register class of operand and regclass of use don't agree!\n";
Chris Lattner01528292007-02-15 18:17:56 +0000596 cerr << "Operand = " << IIOpNum << "\n";
Chris Lattner95ad9432007-02-17 06:38:37 +0000597 cerr << "Op->Val = "; Op.Val->dump(&DAG); cerr << "\n";
Chris Lattner01528292007-02-15 18:17:56 +0000598 cerr << "MI = "; MI->print(cerr);
599 cerr << "VReg = " << VReg << "\n";
600 cerr << "VReg RegClass size = " << VRC->getSize()
Chris Lattner5d4a9f72007-02-15 18:19:15 +0000601 << ", align = " << VRC->getAlignment() << "\n";
Chris Lattner01528292007-02-15 18:17:56 +0000602 cerr << "Expected RegClass size = " << RC->getSize()
Chris Lattner5d4a9f72007-02-15 18:19:15 +0000603 << ", align = " << RC->getAlignment() << "\n";
Chris Lattner01528292007-02-15 18:17:56 +0000604 cerr << "Fatal error, aborting.\n";
605 abort();
606 }
Chris Lattnered18b682006-02-24 18:54:03 +0000607 }
Chris Lattnerb7795802008-03-11 00:59:28 +0000608#endif
Chris Lattnerfec65d52007-12-30 00:51:11 +0000609 } else if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Chris Lattner8019f412007-12-30 00:41:17 +0000610 MI->addOperand(MachineOperand::CreateImm(C->getValue()));
Nate Begemane1795842008-02-14 08:57:00 +0000611 } else if (ConstantFPSDNode *F = dyn_cast<ConstantFPSDNode>(Op)) {
Chris Lattner02a260a2008-04-20 00:41:09 +0000612 ConstantFP *CFP = ConstantFP::get(F->getValueAPF());
Nate Begemane1795842008-02-14 08:57:00 +0000613 MI->addOperand(MachineOperand::CreateFPImm(CFP));
Chris Lattnerfec65d52007-12-30 00:51:11 +0000614 } else if (RegisterSDNode *R = dyn_cast<RegisterSDNode>(Op)) {
Chris Lattner8019f412007-12-30 00:41:17 +0000615 MI->addOperand(MachineOperand::CreateReg(R->getReg(), false));
Chris Lattnerfec65d52007-12-30 00:51:11 +0000616 } else if (GlobalAddressSDNode *TGA = dyn_cast<GlobalAddressSDNode>(Op)) {
617 MI->addOperand(MachineOperand::CreateGA(TGA->getGlobal(),TGA->getOffset()));
618 } else if (BasicBlockSDNode *BB = dyn_cast<BasicBlockSDNode>(Op)) {
619 MI->addOperand(MachineOperand::CreateMBB(BB->getBasicBlock()));
620 } else if (FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(Op)) {
621 MI->addOperand(MachineOperand::CreateFI(FI->getIndex()));
622 } else if (JumpTableSDNode *JT = dyn_cast<JumpTableSDNode>(Op)) {
623 MI->addOperand(MachineOperand::CreateJTI(JT->getIndex()));
624 } else if (ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(Op)) {
Evan Cheng404cb4f2006-02-25 09:54:52 +0000625 int Offset = CP->getOffset();
Chris Lattnered18b682006-02-24 18:54:03 +0000626 unsigned Align = CP->getAlignment();
Evan Chengd6594ae2006-09-12 21:00:35 +0000627 const Type *Type = CP->getType();
Chris Lattnered18b682006-02-24 18:54:03 +0000628 // MachineConstantPool wants an explicit alignment.
629 if (Align == 0) {
Evan Chengde268f72007-01-24 07:03:39 +0000630 Align = TM.getTargetData()->getPreferredTypeAlignmentShift(Type);
Evan Chengf6d039a2007-01-22 23:13:55 +0000631 if (Align == 0) {
Reid Spencerac9dcb92007-02-15 03:39:18 +0000632 // Alignment of vector types. FIXME!
Duncan Sands514ab342007-11-01 20:53:16 +0000633 Align = TM.getTargetData()->getABITypeSize(Type);
Evan Chengf6d039a2007-01-22 23:13:55 +0000634 Align = Log2_64(Align);
Chris Lattner54a30b92006-03-20 01:51:46 +0000635 }
Chris Lattnered18b682006-02-24 18:54:03 +0000636 }
637
Evan Chengd6594ae2006-09-12 21:00:35 +0000638 unsigned Idx;
639 if (CP->isMachineConstantPoolEntry())
640 Idx = ConstPool->getConstantPoolIndex(CP->getMachineCPVal(), Align);
641 else
642 Idx = ConstPool->getConstantPoolIndex(CP->getConstVal(), Align);
Chris Lattnerfec65d52007-12-30 00:51:11 +0000643 MI->addOperand(MachineOperand::CreateCPI(Idx, Offset));
644 } else if (ExternalSymbolSDNode *ES = dyn_cast<ExternalSymbolSDNode>(Op)) {
645 MI->addOperand(MachineOperand::CreateES(ES->getSymbol()));
Chris Lattnered18b682006-02-24 18:54:03 +0000646 } else {
647 assert(Op.getValueType() != MVT::Other &&
648 Op.getValueType() != MVT::Flag &&
649 "Chain and flag operands should occur at end of operand list!");
Chris Lattnerdf375062006-03-10 07:25:12 +0000650 unsigned VReg = getVR(Op, VRBaseMap);
Chris Lattner8019f412007-12-30 00:41:17 +0000651 MI->addOperand(MachineOperand::CreateReg(VReg, false));
Chris Lattnered18b682006-02-24 18:54:03 +0000652
Chris Lattner02b6d252008-03-09 08:49:15 +0000653 // Verify that it is right. Note that the reg class of the physreg and the
654 // vreg don't necessarily need to match, but the target copy insertion has
655 // to be able to handle it. This handles things like copies from ST(0) to
656 // an FP vreg on x86.
Dan Gohman6f0d0242008-02-10 18:45:23 +0000657 assert(TargetRegisterInfo::isVirtualRegister(VReg) && "Not a vreg?");
Chris Lattnerc5733ac2008-03-11 03:14:42 +0000658 if (II && !II->isVariadic()) {
Chris Lattner02b6d252008-03-09 08:49:15 +0000659 assert(getInstrOperandRegClass(TRI, TII, *II, IIOpNum) &&
660 "Don't have operand info for this instruction!");
Chris Lattnered18b682006-02-24 18:54:03 +0000661 }
662 }
663
664}
665
Dan Gohman36b5c132008-04-07 19:35:22 +0000666void ScheduleDAG::AddMemOperand(MachineInstr *MI, const MachineMemOperand &MO) {
Dan Gohman69de1932008-02-06 22:27:42 +0000667 MI->addMemOperand(MO);
668}
669
Christopher Lambe24f8f12007-07-26 08:12:07 +0000670// Returns the Register Class of a subregister
671static const TargetRegisterClass *getSubRegisterRegClass(
672 const TargetRegisterClass *TRC,
673 unsigned SubIdx) {
674 // Pick the register class of the subregister
Dan Gohman6f0d0242008-02-10 18:45:23 +0000675 TargetRegisterInfo::regclass_iterator I =
676 TRC->subregclasses_begin() + SubIdx-1;
Christopher Lambe24f8f12007-07-26 08:12:07 +0000677 assert(I < TRC->subregclasses_end() &&
678 "Invalid subregister index for register class");
679 return *I;
680}
681
682static const TargetRegisterClass *getSuperregRegisterClass(
683 const TargetRegisterClass *TRC,
684 unsigned SubIdx,
Duncan Sands83ec4b62008-06-06 12:08:01 +0000685 MVT VT) {
Christopher Lambe24f8f12007-07-26 08:12:07 +0000686 // Pick the register class of the superegister for this type
Dan Gohman6f0d0242008-02-10 18:45:23 +0000687 for (TargetRegisterInfo::regclass_iterator I = TRC->superregclasses_begin(),
Christopher Lambe24f8f12007-07-26 08:12:07 +0000688 E = TRC->superregclasses_end(); I != E; ++I)
689 if ((*I)->hasType(VT) && getSubRegisterRegClass(*I, SubIdx) == TRC)
690 return *I;
691 assert(false && "Couldn't find the register class");
692 return 0;
693}
694
695/// EmitSubregNode - Generate machine code for subreg nodes.
696///
697void ScheduleDAG::EmitSubregNode(SDNode *Node,
Roman Levenstein9cac5252008-04-16 16:15:27 +0000698 DenseMap<SDOperand, unsigned> &VRBaseMap) {
Christopher Lambe24f8f12007-07-26 08:12:07 +0000699 unsigned VRBase = 0;
700 unsigned Opc = Node->getTargetOpcode();
Christopher Lambc9298232008-03-16 03:12:01 +0000701
702 // If the node is only used by a CopyToReg and the dest reg is a vreg, use
703 // the CopyToReg'd destination register instead of creating a new vreg.
704 for (SDNode::use_iterator UI = Node->use_begin(), E = Node->use_end();
705 UI != E; ++UI) {
Roman Levensteindc1adac2008-04-07 10:06:32 +0000706 SDNode *Use = UI->getUser();
Christopher Lambc9298232008-03-16 03:12:01 +0000707 if (Use->getOpcode() == ISD::CopyToReg &&
708 Use->getOperand(2).Val == Node) {
709 unsigned DestReg = cast<RegisterSDNode>(Use->getOperand(1))->getReg();
710 if (TargetRegisterInfo::isVirtualRegister(DestReg)) {
711 VRBase = DestReg;
712 break;
Christopher Lambe24f8f12007-07-26 08:12:07 +0000713 }
714 }
Christopher Lambc9298232008-03-16 03:12:01 +0000715 }
716
717 if (Opc == TargetInstrInfo::EXTRACT_SUBREG) {
Christopher Lambe24f8f12007-07-26 08:12:07 +0000718 unsigned SubIdx = cast<ConstantSDNode>(Node->getOperand(1))->getValue();
Christopher Lambe24f8f12007-07-26 08:12:07 +0000719
Christopher Lambe24f8f12007-07-26 08:12:07 +0000720 // Create the extract_subreg machine instruction.
Evan Cheng8a50f1f2008-04-03 16:36:07 +0000721 MachineInstr *MI = BuildMI(TII->get(TargetInstrInfo::EXTRACT_SUBREG));
Christopher Lambe24f8f12007-07-26 08:12:07 +0000722
723 // Figure out the register class to create for the destreg.
724 unsigned VReg = getVR(Node->getOperand(0), VRBaseMap);
Evan Cheng9e233362008-03-12 22:19:41 +0000725 const TargetRegisterClass *TRC = MRI.getRegClass(VReg);
Christopher Lambe24f8f12007-07-26 08:12:07 +0000726 const TargetRegisterClass *SRC = getSubRegisterRegClass(TRC, SubIdx);
727
728 if (VRBase) {
729 // Grab the destination register
Evan Cheng50871242008-05-14 20:07:51 +0000730#ifndef NDEBUG
Evan Cheng9e233362008-03-12 22:19:41 +0000731 const TargetRegisterClass *DRC = MRI.getRegClass(VRBase);
Christopher Lamb175e8152008-01-31 07:09:08 +0000732 assert(SRC && DRC && SRC == DRC &&
Christopher Lambe24f8f12007-07-26 08:12:07 +0000733 "Source subregister and destination must have the same class");
Evan Cheng50871242008-05-14 20:07:51 +0000734#endif
Christopher Lambe24f8f12007-07-26 08:12:07 +0000735 } else {
736 // Create the reg
Christopher Lamb175e8152008-01-31 07:09:08 +0000737 assert(SRC && "Couldn't find source register class");
Evan Cheng9e233362008-03-12 22:19:41 +0000738 VRBase = MRI.createVirtualRegister(SRC);
Christopher Lambe24f8f12007-07-26 08:12:07 +0000739 }
740
741 // Add def, source, and subreg index
Chris Lattner8019f412007-12-30 00:41:17 +0000742 MI->addOperand(MachineOperand::CreateReg(VRBase, true));
Christopher Lambe24f8f12007-07-26 08:12:07 +0000743 AddOperand(MI, Node->getOperand(0), 0, 0, VRBaseMap);
Chris Lattnerfec65d52007-12-30 00:51:11 +0000744 MI->addOperand(MachineOperand::CreateImm(SubIdx));
Evan Cheng8a50f1f2008-04-03 16:36:07 +0000745 BB->push_back(MI);
Christopher Lambc9298232008-03-16 03:12:01 +0000746 } else if (Opc == TargetInstrInfo::INSERT_SUBREG ||
747 Opc == TargetInstrInfo::SUBREG_TO_REG) {
Christopher Lamb1fab4a62008-03-11 10:09:17 +0000748 SDOperand N0 = Node->getOperand(0);
749 SDOperand N1 = Node->getOperand(1);
750 SDOperand N2 = Node->getOperand(2);
751 unsigned SubReg = getVR(N1, VRBaseMap);
752 unsigned SubIdx = cast<ConstantSDNode>(N2)->getValue();
Christopher Lambe24f8f12007-07-26 08:12:07 +0000753
Christopher Lambe24f8f12007-07-26 08:12:07 +0000754
755 // Figure out the register class to create for the destreg.
756 const TargetRegisterClass *TRC = 0;
757 if (VRBase) {
Evan Cheng9e233362008-03-12 22:19:41 +0000758 TRC = MRI.getRegClass(VRBase);
Christopher Lambe24f8f12007-07-26 08:12:07 +0000759 } else {
Evan Cheng9e233362008-03-12 22:19:41 +0000760 TRC = getSuperregRegisterClass(MRI.getRegClass(SubReg), SubIdx,
Christopher Lambe24f8f12007-07-26 08:12:07 +0000761 Node->getValueType(0));
762 assert(TRC && "Couldn't determine register class for insert_subreg");
Evan Cheng9e233362008-03-12 22:19:41 +0000763 VRBase = MRI.createVirtualRegister(TRC); // Create the reg
Christopher Lambe24f8f12007-07-26 08:12:07 +0000764 }
765
Christopher Lambc9298232008-03-16 03:12:01 +0000766 // Create the insert_subreg or subreg_to_reg machine instruction.
Evan Cheng8a50f1f2008-04-03 16:36:07 +0000767 MachineInstr *MI = BuildMI(TII->get(Opc));
Chris Lattner8019f412007-12-30 00:41:17 +0000768 MI->addOperand(MachineOperand::CreateReg(VRBase, true));
Christopher Lamb1fab4a62008-03-11 10:09:17 +0000769
Christopher Lambc9298232008-03-16 03:12:01 +0000770 // If creating a subreg_to_reg, then the first input operand
771 // is an implicit value immediate, otherwise it's a register
772 if (Opc == TargetInstrInfo::SUBREG_TO_REG) {
773 const ConstantSDNode *SD = cast<ConstantSDNode>(N0);
Christopher Lamb1fab4a62008-03-11 10:09:17 +0000774 MI->addOperand(MachineOperand::CreateImm(SD->getValue()));
Christopher Lambc9298232008-03-16 03:12:01 +0000775 } else
Christopher Lamb1fab4a62008-03-11 10:09:17 +0000776 AddOperand(MI, N0, 0, 0, VRBaseMap);
777 // Add the subregster being inserted
778 AddOperand(MI, N1, 0, 0, VRBaseMap);
Chris Lattnerfec65d52007-12-30 00:51:11 +0000779 MI->addOperand(MachineOperand::CreateImm(SubIdx));
Evan Cheng8a50f1f2008-04-03 16:36:07 +0000780 BB->push_back(MI);
Christopher Lambe24f8f12007-07-26 08:12:07 +0000781 } else
Christopher Lambc9298232008-03-16 03:12:01 +0000782 assert(0 && "Node is not insert_subreg, extract_subreg, or subreg_to_reg");
Christopher Lambe24f8f12007-07-26 08:12:07 +0000783
784 bool isNew = VRBaseMap.insert(std::make_pair(SDOperand(Node,0), VRBase));
Evan Cheng97e60d92008-05-14 21:08:07 +0000785 isNew = isNew; // Silence compiler warning.
Christopher Lambe24f8f12007-07-26 08:12:07 +0000786 assert(isNew && "Node emitted out of order - early");
787}
788
Jim Laskeyb6d4c2c2005-09-30 19:15:27 +0000789/// EmitNode - Generate machine code for an node and needed dependencies.
Jim Laskeye6b90fb2005-09-26 21:57:04 +0000790///
Dan Gohman4c8c8302008-06-21 15:52:51 +0000791void ScheduleDAG::EmitNode(SDNode *Node, bool IsClone,
Roman Levenstein9cac5252008-04-16 16:15:27 +0000792 DenseMap<SDOperand, unsigned> &VRBaseMap) {
Jim Laskeyb6d4c2c2005-09-30 19:15:27 +0000793 // If machine instruction
794 if (Node->isTargetOpcode()) {
795 unsigned Opc = Node->getTargetOpcode();
Christopher Lambe24f8f12007-07-26 08:12:07 +0000796
797 // Handle subreg insert/extract specially
798 if (Opc == TargetInstrInfo::EXTRACT_SUBREG ||
Christopher Lambc9298232008-03-16 03:12:01 +0000799 Opc == TargetInstrInfo::INSERT_SUBREG ||
800 Opc == TargetInstrInfo::SUBREG_TO_REG) {
Christopher Lambe24f8f12007-07-26 08:12:07 +0000801 EmitSubregNode(Node, VRBaseMap);
802 return;
803 }
Evan Cheng8a50f1f2008-04-03 16:36:07 +0000804
805 if (Opc == TargetInstrInfo::IMPLICIT_DEF)
806 // We want a unique VR for each IMPLICIT_DEF use.
807 return;
Christopher Lambe24f8f12007-07-26 08:12:07 +0000808
Chris Lattner749c6f62008-01-07 07:27:27 +0000809 const TargetInstrDesc &II = TII->get(Opc);
Jim Laskeyb6d4c2c2005-09-30 19:15:27 +0000810 unsigned NumResults = CountResults(Node);
811 unsigned NodeOperands = CountOperands(Node);
Dan Gohman42a77882008-02-16 00:36:48 +0000812 unsigned MemOperandsEnd = ComputeMemOperandsEnd(Node);
Chris Lattner349c4952008-01-07 03:13:06 +0000813 bool HasPhysRegOuts = (NumResults > II.getNumDefs()) &&
814 II.getImplicitDefs() != 0;
Chris Lattnerda8abb02005-09-01 18:44:10 +0000815#ifndef NDEBUG
Evan Cheng50871242008-05-14 20:07:51 +0000816 unsigned NumMIOperands = NodeOperands + NumResults;
Chris Lattner349c4952008-01-07 03:13:06 +0000817 assert((II.getNumOperands() == NumMIOperands ||
Chris Lattner8f707e12008-01-07 05:19:29 +0000818 HasPhysRegOuts || II.isVariadic()) &&
Chris Lattner2d973e42005-08-18 20:07:59 +0000819 "#operands for dag node doesn't match .td file!");
Chris Lattnerca6aa2f2005-08-19 01:01:34 +0000820#endif
Chris Lattner2d973e42005-08-18 20:07:59 +0000821
822 // Create the new machine instruction.
Evan Cheng8a50f1f2008-04-03 16:36:07 +0000823 MachineInstr *MI = BuildMI(II);
Chris Lattner2d973e42005-08-18 20:07:59 +0000824
825 // Add result register values for things that are defined by this
826 // instruction.
Evan Chengaf825c82007-07-10 07:08:32 +0000827 if (NumResults)
Evan Cheng84097472007-08-02 00:28:15 +0000828 CreateVirtualRegisters(Node, MI, II, VRBaseMap);
Jim Laskeyb6d4c2c2005-09-30 19:15:27 +0000829
830 // Emit all of the actual operands of this instruction, adding them to the
831 // instruction as appropriate.
Chris Lattnered18b682006-02-24 18:54:03 +0000832 for (unsigned i = 0; i != NodeOperands; ++i)
Chris Lattner349c4952008-01-07 03:13:06 +0000833 AddOperand(MI, Node->getOperand(i), i+II.getNumDefs(), &II, VRBaseMap);
Evan Cheng13d41b92006-05-12 01:58:24 +0000834
Dan Gohman69de1932008-02-06 22:27:42 +0000835 // Emit all of the memory operands of this instruction
Dan Gohman42a77882008-02-16 00:36:48 +0000836 for (unsigned i = NodeOperands; i != MemOperandsEnd; ++i)
Dan Gohman69de1932008-02-06 22:27:42 +0000837 AddMemOperand(MI, cast<MemOperandSDNode>(Node->getOperand(i))->MO);
838
Evan Cheng13d41b92006-05-12 01:58:24 +0000839 // Commute node if it has been determined to be profitable.
840 if (CommuteSet.count(Node)) {
841 MachineInstr *NewMI = TII->commuteInstruction(MI);
842 if (NewMI == 0)
Bill Wendling832171c2006-12-07 20:04:42 +0000843 DOUT << "Sched: COMMUTING FAILED!\n";
Evan Cheng13d41b92006-05-12 01:58:24 +0000844 else {
Bill Wendling832171c2006-12-07 20:04:42 +0000845 DOUT << "Sched: COMMUTED TO: " << *NewMI;
Evan Cheng4c6f2f92006-05-31 18:03:39 +0000846 if (MI != NewMI) {
847 delete MI;
848 MI = NewMI;
849 }
Evan Cheng643afa52008-02-28 07:40:24 +0000850 ++NumCommutes;
Evan Cheng13d41b92006-05-12 01:58:24 +0000851 }
852 }
853
Evan Cheng1b08bbc2008-02-01 09:10:45 +0000854 if (II.usesCustomDAGSchedInsertionHook())
Evan Cheng6b2cf282008-01-30 19:35:32 +0000855 // Insert this instruction into the basic block using a target
856 // specific inserter which may returns a new basic block.
Evan Cheng8a50f1f2008-04-03 16:36:07 +0000857 BB = TLI->EmitInstrWithCustomInserter(MI, BB);
Evan Cheng6b2cf282008-01-30 19:35:32 +0000858 else
859 BB->push_back(MI);
Evan Cheng84097472007-08-02 00:28:15 +0000860
861 // Additional results must be an physical register def.
862 if (HasPhysRegOuts) {
Chris Lattner349c4952008-01-07 03:13:06 +0000863 for (unsigned i = II.getNumDefs(); i < NumResults; ++i) {
864 unsigned Reg = II.getImplicitDefs()[i - II.getNumDefs()];
Evan Cheng33d55952007-08-02 05:29:38 +0000865 if (Node->hasAnyUseOfValue(i))
Dan Gohman4c8c8302008-06-21 15:52:51 +0000866 EmitCopyFromReg(Node, i, IsClone, Reg, VRBaseMap);
Evan Cheng84097472007-08-02 00:28:15 +0000867 }
868 }
Jim Laskeyb6d4c2c2005-09-30 19:15:27 +0000869 } else {
870 switch (Node->getOpcode()) {
871 default:
Jim Laskey16d42c62006-07-11 18:25:13 +0000872#ifndef NDEBUG
Dan Gohmanb5bec2b2007-06-19 14:13:56 +0000873 Node->dump(&DAG);
Jim Laskey16d42c62006-07-11 18:25:13 +0000874#endif
Jim Laskeyb6d4c2c2005-09-30 19:15:27 +0000875 assert(0 && "This target-independent node should have been selected!");
Dan Gohman80792f32008-04-15 01:22:18 +0000876 break;
877 case ISD::EntryToken:
878 assert(0 && "EntryToken should have been excluded from the schedule!");
879 break;
880 case ISD::TokenFactor: // fall thru
Jim Laskey1ee29252007-01-26 14:34:52 +0000881 case ISD::LABEL:
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;
885 case ISD::CopyToReg: {
Chris Lattnerf30e1cf2008-03-09 09:15:31 +0000886 unsigned SrcReg;
887 SDOperand SrcVal = Node->getOperand(2);
888 if (RegisterSDNode *R = dyn_cast<RegisterSDNode>(SrcVal))
889 SrcReg = R->getReg();
Evan Cheng489a87c2007-01-05 20:59:06 +0000890 else
Chris Lattnerf30e1cf2008-03-09 09:15:31 +0000891 SrcReg = getVR(SrcVal, VRBaseMap);
892
Chris Lattnera4176522005-10-30 18:54:27 +0000893 unsigned DestReg = cast<RegisterSDNode>(Node->getOperand(1))->getReg();
Chris Lattnerf30e1cf2008-03-09 09:15:31 +0000894 if (SrcReg == DestReg) // Coalesced away the copy? Ignore.
895 break;
896
897 const TargetRegisterClass *SrcTRC = 0, *DstTRC = 0;
898 // Get the register classes of the src/dst.
899 if (TargetRegisterInfo::isVirtualRegister(SrcReg))
Evan Cheng9e233362008-03-12 22:19:41 +0000900 SrcTRC = MRI.getRegClass(SrcReg);
Chris Lattnerf30e1cf2008-03-09 09:15:31 +0000901 else
Evan Cheng676dd7c2008-03-11 07:19:34 +0000902 SrcTRC = TRI->getPhysicalRegisterRegClass(SrcReg,SrcVal.getValueType());
Chris Lattnerf30e1cf2008-03-09 09:15:31 +0000903
904 if (TargetRegisterInfo::isVirtualRegister(DestReg))
Evan Cheng9e233362008-03-12 22:19:41 +0000905 DstTRC = MRI.getRegClass(DestReg);
Chris Lattnerf30e1cf2008-03-09 09:15:31 +0000906 else
Evan Cheng676dd7c2008-03-11 07:19:34 +0000907 DstTRC = TRI->getPhysicalRegisterRegClass(DestReg,
908 Node->getOperand(1).getValueType());
Chris Lattnerf30e1cf2008-03-09 09:15:31 +0000909 TII->copyRegToReg(*BB, BB->end(), DestReg, SrcReg, DstTRC, SrcTRC);
Jim Laskeyb6d4c2c2005-09-30 19:15:27 +0000910 break;
911 }
912 case ISD::CopyFromReg: {
913 unsigned SrcReg = cast<RegisterSDNode>(Node->getOperand(1))->getReg();
Dan Gohman4c8c8302008-06-21 15:52:51 +0000914 EmitCopyFromReg(Node, 0, IsClone, SrcReg, VRBaseMap);
Jim Laskeyb6d4c2c2005-09-30 19:15:27 +0000915 break;
916 }
Chris Lattneracc43bf2006-01-26 23:28:04 +0000917 case ISD::INLINEASM: {
918 unsigned NumOps = Node->getNumOperands();
919 if (Node->getOperand(NumOps-1).getValueType() == MVT::Flag)
920 --NumOps; // Ignore the flag operand.
921
922 // Create the inline asm machine instruction.
Evan Cheng8a50f1f2008-04-03 16:36:07 +0000923 MachineInstr *MI = BuildMI(TII->get(TargetInstrInfo::INLINEASM));
Chris Lattneracc43bf2006-01-26 23:28:04 +0000924
925 // Add the asm string as an external symbol operand.
926 const char *AsmStr =
927 cast<ExternalSymbolSDNode>(Node->getOperand(1))->getSymbol();
Chris Lattnerfec65d52007-12-30 00:51:11 +0000928 MI->addOperand(MachineOperand::CreateES(AsmStr));
Chris Lattneracc43bf2006-01-26 23:28:04 +0000929
930 // Add all of the operand registers to the instruction.
Chris Lattnerc3a9f8d2006-02-23 19:21:04 +0000931 for (unsigned i = 2; i != NumOps;) {
932 unsigned Flags = cast<ConstantSDNode>(Node->getOperand(i))->getValue();
Chris Lattnerfd6d2822006-02-24 19:18:20 +0000933 unsigned NumVals = Flags >> 3;
Chris Lattnerc3a9f8d2006-02-23 19:21:04 +0000934
Chris Lattnerfec65d52007-12-30 00:51:11 +0000935 MI->addOperand(MachineOperand::CreateImm(Flags));
Chris Lattnerc3a9f8d2006-02-23 19:21:04 +0000936 ++i; // Skip the ID value.
937
938 switch (Flags & 7) {
Chris Lattneracc43bf2006-01-26 23:28:04 +0000939 default: assert(0 && "Bad flags!");
Chris Lattnerc3a9f8d2006-02-23 19:21:04 +0000940 case 1: // Use of register.
Chris Lattnerfd6d2822006-02-24 19:18:20 +0000941 for (; NumVals; --NumVals, ++i) {
Chris Lattnerc3a9f8d2006-02-23 19:21:04 +0000942 unsigned Reg = cast<RegisterSDNode>(Node->getOperand(i))->getReg();
Chris Lattner8019f412007-12-30 00:41:17 +0000943 MI->addOperand(MachineOperand::CreateReg(Reg, false));
Chris Lattnerc3a9f8d2006-02-23 19:21:04 +0000944 }
Chris Lattnerdc19b702006-02-04 02:26:14 +0000945 break;
Chris Lattnerc3a9f8d2006-02-23 19:21:04 +0000946 case 2: // Def of register.
Chris Lattnerfd6d2822006-02-24 19:18:20 +0000947 for (; NumVals; --NumVals, ++i) {
Chris Lattnerc3a9f8d2006-02-23 19:21:04 +0000948 unsigned Reg = cast<RegisterSDNode>(Node->getOperand(i))->getReg();
Chris Lattner8019f412007-12-30 00:41:17 +0000949 MI->addOperand(MachineOperand::CreateReg(Reg, true));
Chris Lattnerc3a9f8d2006-02-23 19:21:04 +0000950 }
Chris Lattnerdc19b702006-02-04 02:26:14 +0000951 break;
Chris Lattnerdc19b702006-02-04 02:26:14 +0000952 case 3: { // Immediate.
Chris Lattner7df31dc2007-08-25 00:53:07 +0000953 for (; NumVals; --NumVals, ++i) {
954 if (ConstantSDNode *CS =
955 dyn_cast<ConstantSDNode>(Node->getOperand(i))) {
Chris Lattner8019f412007-12-30 00:41:17 +0000956 MI->addOperand(MachineOperand::CreateImm(CS->getValue()));
Dale Johanneseneb57ea72007-11-05 21:20:28 +0000957 } else if (GlobalAddressSDNode *GA =
958 dyn_cast<GlobalAddressSDNode>(Node->getOperand(i))) {
Chris Lattnerfec65d52007-12-30 00:51:11 +0000959 MI->addOperand(MachineOperand::CreateGA(GA->getGlobal(),
960 GA->getOffset()));
Dale Johanneseneb57ea72007-11-05 21:20:28 +0000961 } else {
Chris Lattnerfec65d52007-12-30 00:51:11 +0000962 BasicBlockSDNode *BB =cast<BasicBlockSDNode>(Node->getOperand(i));
963 MI->addOperand(MachineOperand::CreateMBB(BB->getBasicBlock()));
Chris Lattner7df31dc2007-08-25 00:53:07 +0000964 }
Chris Lattnerefa46ce2006-10-31 20:01:56 +0000965 }
Chris Lattnerdc19b702006-02-04 02:26:14 +0000966 break;
967 }
Chris Lattnerfd6d2822006-02-24 19:18:20 +0000968 case 4: // Addressing mode.
969 // The addressing mode has been selected, just add all of the
970 // operands to the machine instruction.
971 for (; NumVals; --NumVals, ++i)
Chris Lattnerdf375062006-03-10 07:25:12 +0000972 AddOperand(MI, Node->getOperand(i), 0, 0, VRBaseMap);
Chris Lattnerfd6d2822006-02-24 19:18:20 +0000973 break;
Chris Lattnerdc19b702006-02-04 02:26:14 +0000974 }
Chris Lattneracc43bf2006-01-26 23:28:04 +0000975 }
Evan Cheng8a50f1f2008-04-03 16:36:07 +0000976 BB->push_back(MI);
Chris Lattneracc43bf2006-01-26 23:28:04 +0000977 break;
978 }
Jim Laskeyb6d4c2c2005-09-30 19:15:27 +0000979 }
980 }
Jim Laskeyb6d4c2c2005-09-30 19:15:27 +0000981}
982
Chris Lattnera93dfcd2006-03-05 23:51:47 +0000983void ScheduleDAG::EmitNoop() {
984 TII->insertNoop(*BB, BB->end());
985}
986
Chris Lattnerd9c4c452008-03-09 07:51:01 +0000987void ScheduleDAG::EmitCrossRCCopy(SUnit *SU,
988 DenseMap<SUnit*, unsigned> &VRBaseMap) {
Evan Cheng42d60272007-09-26 21:36:17 +0000989 for (SUnit::const_pred_iterator I = SU->Preds.begin(), E = SU->Preds.end();
990 I != E; ++I) {
991 if (I->isCtrl) continue; // ignore chain preds
992 if (!I->Dep->Node) {
993 // Copy to physical register.
994 DenseMap<SUnit*, unsigned>::iterator VRI = VRBaseMap.find(I->Dep);
995 assert(VRI != VRBaseMap.end() && "Node emitted out of order - late");
996 // Find the destination physical register.
997 unsigned Reg = 0;
998 for (SUnit::const_succ_iterator II = SU->Succs.begin(),
999 EE = SU->Succs.end(); II != EE; ++II) {
1000 if (I->Reg) {
1001 Reg = I->Reg;
1002 break;
1003 }
1004 }
1005 assert(I->Reg && "Unknown physical register!");
Owen Andersond10fd972007-12-31 06:32:00 +00001006 TII->copyRegToReg(*BB, BB->end(), Reg, VRI->second,
Evan Cheng42d60272007-09-26 21:36:17 +00001007 SU->CopyDstRC, SU->CopySrcRC);
1008 } else {
1009 // Copy from physical register.
1010 assert(I->Reg && "Unknown physical register!");
Evan Cheng9e233362008-03-12 22:19:41 +00001011 unsigned VRBase = MRI.createVirtualRegister(SU->CopyDstRC);
Evan Cheng42d60272007-09-26 21:36:17 +00001012 bool isNew = VRBaseMap.insert(std::make_pair(SU, VRBase));
Evan Cheng97e60d92008-05-14 21:08:07 +00001013 isNew = isNew; // Silence compiler warning.
Evan Cheng42d60272007-09-26 21:36:17 +00001014 assert(isNew && "Node emitted out of order - early");
Owen Andersond10fd972007-12-31 06:32:00 +00001015 TII->copyRegToReg(*BB, BB->end(), VRBase, I->Reg,
Evan Cheng42d60272007-09-26 21:36:17 +00001016 SU->CopyDstRC, SU->CopySrcRC);
1017 }
1018 break;
1019 }
1020}
1021
Evan Cheng9e233362008-03-12 22:19:41 +00001022/// EmitLiveInCopy - Emit a copy for a live in physical register. If the
1023/// physical register has only a single copy use, then coalesced the copy
Evan Chengdb2d7732008-03-14 00:14:55 +00001024/// if possible.
1025void ScheduleDAG::EmitLiveInCopy(MachineBasicBlock *MBB,
1026 MachineBasicBlock::iterator &InsertPos,
1027 unsigned VirtReg, unsigned PhysReg,
1028 const TargetRegisterClass *RC,
1029 DenseMap<MachineInstr*, unsigned> &CopyRegMap){
Evan Cheng9e233362008-03-12 22:19:41 +00001030 unsigned NumUses = 0;
1031 MachineInstr *UseMI = NULL;
1032 for (MachineRegisterInfo::use_iterator UI = MRI.use_begin(VirtReg),
1033 UE = MRI.use_end(); UI != UE; ++UI) {
1034 UseMI = &*UI;
1035 if (++NumUses > 1)
1036 break;
1037 }
1038
1039 // If the number of uses is not one, or the use is not a move instruction,
Evan Chengdb2d7732008-03-14 00:14:55 +00001040 // don't coalesce. Also, only coalesce away a virtual register to virtual
1041 // register copy.
1042 bool Coalesced = false;
Evan Cheng9e233362008-03-12 22:19:41 +00001043 unsigned SrcReg, DstReg;
Evan Chengdb2d7732008-03-14 00:14:55 +00001044 if (NumUses == 1 &&
1045 TII->isMoveInstr(*UseMI, SrcReg, DstReg) &&
1046 TargetRegisterInfo::isVirtualRegister(DstReg)) {
1047 VirtReg = DstReg;
1048 Coalesced = true;
Evan Cheng9e233362008-03-12 22:19:41 +00001049 }
1050
Evan Chengdb2d7732008-03-14 00:14:55 +00001051 // Now find an ideal location to insert the copy.
1052 MachineBasicBlock::iterator Pos = InsertPos;
1053 while (Pos != MBB->begin()) {
1054 MachineInstr *PrevMI = prior(Pos);
1055 DenseMap<MachineInstr*, unsigned>::iterator RI = CopyRegMap.find(PrevMI);
1056 // copyRegToReg might emit multiple instructions to do a copy.
1057 unsigned CopyDstReg = (RI == CopyRegMap.end()) ? 0 : RI->second;
1058 if (CopyDstReg && !TRI->regsOverlap(CopyDstReg, PhysReg))
1059 // This is what the BB looks like right now:
1060 // r1024 = mov r0
1061 // ...
1062 // r1 = mov r1024
1063 //
1064 // We want to insert "r1025 = mov r1". Inserting this copy below the
1065 // move to r1024 makes it impossible for that move to be coalesced.
1066 //
1067 // r1025 = mov r1
1068 // r1024 = mov r0
1069 // ...
1070 // r1 = mov 1024
1071 // r2 = mov 1025
1072 break; // Woot! Found a good location.
1073 --Pos;
1074 }
1075
1076 TII->copyRegToReg(*MBB, Pos, VirtReg, PhysReg, RC, RC);
1077 CopyRegMap.insert(std::make_pair(prior(Pos), VirtReg));
1078 if (Coalesced) {
Evan Cheng9e233362008-03-12 22:19:41 +00001079 if (&*InsertPos == UseMI) ++InsertPos;
1080 MBB->erase(UseMI);
Evan Cheng9e233362008-03-12 22:19:41 +00001081 }
Evan Cheng9e233362008-03-12 22:19:41 +00001082}
1083
1084/// EmitLiveInCopies - If this is the first basic block in the function,
1085/// and if it has live ins that need to be copied into vregs, emit the
1086/// copies into the top of the block.
1087void ScheduleDAG::EmitLiveInCopies(MachineBasicBlock *MBB) {
Evan Chengdb2d7732008-03-14 00:14:55 +00001088 DenseMap<MachineInstr*, unsigned> CopyRegMap;
Evan Cheng9e233362008-03-12 22:19:41 +00001089 MachineBasicBlock::iterator InsertPos = MBB->begin();
1090 for (MachineRegisterInfo::livein_iterator LI = MRI.livein_begin(),
1091 E = MRI.livein_end(); LI != E; ++LI)
1092 if (LI->second) {
1093 const TargetRegisterClass *RC = MRI.getRegClass(LI->second);
Evan Chengdb2d7732008-03-14 00:14:55 +00001094 EmitLiveInCopy(MBB, InsertPos, LI->second, LI->first, RC, CopyRegMap);
Evan Cheng9e233362008-03-12 22:19:41 +00001095 }
1096}
1097
Evan Chenge165a782006-05-11 23:55:42 +00001098/// EmitSchedule - Emit the machine code in scheduled order.
1099void ScheduleDAG::EmitSchedule() {
Evan Cheng9e233362008-03-12 22:19:41 +00001100 bool isEntryBB = &MF->front() == BB;
1101
1102 if (isEntryBB && !SchedLiveInCopies) {
1103 // If this is the first basic block in the function, and if it has live ins
1104 // that need to be copied into vregs, emit the copies into the top of the
1105 // block before emitting the code for the block.
1106 for (MachineRegisterInfo::livein_iterator LI = MRI.livein_begin(),
1107 E = MRI.livein_end(); LI != E; ++LI)
Evan Cheng9efce632007-09-26 06:25:56 +00001108 if (LI->second) {
Evan Cheng9e233362008-03-12 22:19:41 +00001109 const TargetRegisterClass *RC = MRI.getRegClass(LI->second);
Evan Cheng6b2cf282008-01-30 19:35:32 +00001110 TII->copyRegToReg(*MF->begin(), MF->begin()->end(), LI->second,
Evan Cheng9efce632007-09-26 06:25:56 +00001111 LI->first, RC, RC);
1112 }
Chris Lattner96645412006-05-16 06:10:58 +00001113 }
Evan Cheng9e233362008-03-12 22:19:41 +00001114
Chris Lattner96645412006-05-16 06:10:58 +00001115 // Finally, emit the code for all of the scheduled instructions.
Roman Levenstein9cac5252008-04-16 16:15:27 +00001116 DenseMap<SDOperand, unsigned> VRBaseMap;
Evan Cheng42d60272007-09-26 21:36:17 +00001117 DenseMap<SUnit*, unsigned> CopyVRBaseMap;
Evan Chenge165a782006-05-11 23:55:42 +00001118 for (unsigned i = 0, e = Sequence.size(); i != e; i++) {
Evan Cheng8a50f1f2008-04-03 16:36:07 +00001119 SUnit *SU = Sequence[i];
1120 if (!SU) {
Evan Chenge165a782006-05-11 23:55:42 +00001121 // Null SUnit* is a noop.
1122 EmitNoop();
Evan Cheng8a50f1f2008-04-03 16:36:07 +00001123 continue;
Evan Chenge165a782006-05-11 23:55:42 +00001124 }
Evan Cheng8a50f1f2008-04-03 16:36:07 +00001125 for (unsigned j = 0, ee = SU->FlaggedNodes.size(); j != ee; ++j)
Dan Gohman4c8c8302008-06-21 15:52:51 +00001126 EmitNode(SU->FlaggedNodes[j], SU->OrigNode != SU, VRBaseMap);
Evan Cheng8a50f1f2008-04-03 16:36:07 +00001127 if (!SU->Node)
1128 EmitCrossRCCopy(SU, CopyVRBaseMap);
1129 else
Dan Gohman4c8c8302008-06-21 15:52:51 +00001130 EmitNode(SU->Node, SU->OrigNode != SU, VRBaseMap);
Evan Chenge165a782006-05-11 23:55:42 +00001131 }
Evan Cheng9e233362008-03-12 22:19:41 +00001132
1133 if (isEntryBB && SchedLiveInCopies)
1134 EmitLiveInCopies(MF->begin());
Evan Chenge165a782006-05-11 23:55:42 +00001135}
1136
1137/// dump - dump the schedule.
1138void ScheduleDAG::dumpSchedule() const {
1139 for (unsigned i = 0, e = Sequence.size(); i != e; i++) {
1140 if (SUnit *SU = Sequence[i])
1141 SU->dump(&DAG);
1142 else
Bill Wendling832171c2006-12-07 20:04:42 +00001143 cerr << "**** NOOP ****\n";
Evan Chenge165a782006-05-11 23:55:42 +00001144 }
1145}
1146
1147
Evan Chenga9c20912006-01-21 02:32:06 +00001148/// Run - perform scheduling.
1149///
1150MachineBasicBlock *ScheduleDAG::Run() {
Evan Chenga9c20912006-01-21 02:32:06 +00001151 Schedule();
1152 return BB;
Chris Lattnerd32b2362005-08-18 18:45:24 +00001153}
Evan Cheng4ef10862006-01-23 07:01:07 +00001154
Evan Chenge165a782006-05-11 23:55:42 +00001155/// SUnit - Scheduling unit. It's an wrapper around either a single SDNode or
1156/// a group of nodes flagged together.
1157void SUnit::dump(const SelectionDAG *G) const {
Bill Wendling832171c2006-12-07 20:04:42 +00001158 cerr << "SU(" << NodeNum << "): ";
Evan Cheng42d60272007-09-26 21:36:17 +00001159 if (Node)
1160 Node->dump(G);
1161 else
1162 cerr << "CROSS RC COPY ";
Bill Wendling832171c2006-12-07 20:04:42 +00001163 cerr << "\n";
Evan Chenge165a782006-05-11 23:55:42 +00001164 if (FlaggedNodes.size() != 0) {
1165 for (unsigned i = 0, e = FlaggedNodes.size(); i != e; i++) {
Bill Wendling832171c2006-12-07 20:04:42 +00001166 cerr << " ";
Evan Chenge165a782006-05-11 23:55:42 +00001167 FlaggedNodes[i]->dump(G);
Bill Wendling832171c2006-12-07 20:04:42 +00001168 cerr << "\n";
Evan Chenge165a782006-05-11 23:55:42 +00001169 }
1170 }
1171}
Evan Cheng4ef10862006-01-23 07:01:07 +00001172
Evan Chenge165a782006-05-11 23:55:42 +00001173void SUnit::dumpAll(const SelectionDAG *G) const {
1174 dump(G);
1175
Bill Wendling832171c2006-12-07 20:04:42 +00001176 cerr << " # preds left : " << NumPredsLeft << "\n";
1177 cerr << " # succs left : " << NumSuccsLeft << "\n";
Bill Wendling832171c2006-12-07 20:04:42 +00001178 cerr << " Latency : " << Latency << "\n";
1179 cerr << " Depth : " << Depth << "\n";
1180 cerr << " Height : " << Height << "\n";
Evan Chenge165a782006-05-11 23:55:42 +00001181
1182 if (Preds.size() != 0) {
Bill Wendling832171c2006-12-07 20:04:42 +00001183 cerr << " Predecessors:\n";
Chris Lattner228a18e2006-08-17 00:09:56 +00001184 for (SUnit::const_succ_iterator I = Preds.begin(), E = Preds.end();
1185 I != E; ++I) {
Evan Cheng713a98d2007-09-19 01:38:40 +00001186 if (I->isCtrl)
Bill Wendling832171c2006-12-07 20:04:42 +00001187 cerr << " ch #";
Evan Chenge165a782006-05-11 23:55:42 +00001188 else
Bill Wendling832171c2006-12-07 20:04:42 +00001189 cerr << " val #";
Evan Chenga6fb1b62007-09-25 01:54:36 +00001190 cerr << I->Dep << " - SU(" << I->Dep->NodeNum << ")";
1191 if (I->isSpecial)
1192 cerr << " *";
1193 cerr << "\n";
Evan Chenge165a782006-05-11 23:55:42 +00001194 }
1195 }
1196 if (Succs.size() != 0) {
Bill Wendling832171c2006-12-07 20:04:42 +00001197 cerr << " Successors:\n";
Chris Lattner228a18e2006-08-17 00:09:56 +00001198 for (SUnit::const_succ_iterator I = Succs.begin(), E = Succs.end();
1199 I != E; ++I) {
Evan Cheng713a98d2007-09-19 01:38:40 +00001200 if (I->isCtrl)
Bill Wendling832171c2006-12-07 20:04:42 +00001201 cerr << " ch #";
Evan Chenge165a782006-05-11 23:55:42 +00001202 else
Bill Wendling832171c2006-12-07 20:04:42 +00001203 cerr << " val #";
Evan Chenga6fb1b62007-09-25 01:54:36 +00001204 cerr << I->Dep << " - SU(" << I->Dep->NodeNum << ")";
1205 if (I->isSpecial)
1206 cerr << " *";
1207 cerr << "\n";
Evan Chenge165a782006-05-11 23:55:42 +00001208 }
1209 }
Bill Wendling832171c2006-12-07 20:04:42 +00001210 cerr << "\n";
Evan Chenge165a782006-05-11 23:55:42 +00001211}