blob: 462b94bc3f45e2129f79319be12762555e8fdfa4 [file] [log] [blame]
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001//===---- ScheduleDAG.cpp - Implement the ScheduleDAG class ---------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner081ce942007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This implements a simple two pass scheduler. The first pass attempts to push
11// backward any lengthy instructions and critical paths. The second pass packs
12// instructions into semi-optimal time slots.
13//
14//===----------------------------------------------------------------------===//
15
16#define DEBUG_TYPE "pre-RA-sched"
Nate Begemane2ba64f2008-02-14 08:57:00 +000017#include "llvm/Constants.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000018#include "llvm/Type.h"
19#include "llvm/CodeGen/ScheduleDAG.h"
20#include "llvm/CodeGen/MachineConstantPool.h"
21#include "llvm/CodeGen/MachineFunction.h"
Chris Lattner1b989192007-12-31 04:13:23 +000022#include "llvm/CodeGen/MachineRegisterInfo.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000023#include "llvm/Target/TargetData.h"
24#include "llvm/Target/TargetMachine.h"
25#include "llvm/Target/TargetInstrInfo.h"
26#include "llvm/Target/TargetLowering.h"
Evan Cheng7f6ade32008-02-28 07:40:24 +000027#include "llvm/ADT/Statistic.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000028#include "llvm/Support/Debug.h"
29#include "llvm/Support/MathExtras.h"
30using namespace llvm;
31
Evan Cheng7f6ade32008-02-28 07:40:24 +000032STATISTIC(NumCommutes, "Number of instructions commuted");
33
Chris Lattner1b989192007-12-31 04:13:23 +000034ScheduleDAG::ScheduleDAG(SelectionDAG &dag, MachineBasicBlock *bb,
35 const TargetMachine &tm)
36 : DAG(dag), BB(bb), TM(tm), RegInfo(BB->getParent()->getRegInfo()) {
37 TII = TM.getInstrInfo();
Evan Cheng2d373922008-01-30 19:35:32 +000038 MF = &DAG.getMachineFunction();
Dan Gohman1e57df32008-02-10 18:45:23 +000039 TRI = TM.getRegisterInfo();
Chris Lattner1b989192007-12-31 04:13:23 +000040 ConstPool = BB->getParent()->getConstantPool();
41}
Evan Cheng93f143e2007-09-25 01:54:36 +000042
Evan Cheng93f143e2007-09-25 01:54:36 +000043/// CheckForPhysRegDependency - Check if the dependency between def and use of
44/// a specified operand is a physical register dependency. If so, returns the
45/// register and the cost of copying the register.
46static void CheckForPhysRegDependency(SDNode *Def, SDNode *Use, unsigned Op,
Dan Gohman1e57df32008-02-10 18:45:23 +000047 const TargetRegisterInfo *TRI,
Evan Cheng93f143e2007-09-25 01:54:36 +000048 const TargetInstrInfo *TII,
49 unsigned &PhysReg, int &Cost) {
50 if (Op != 2 || Use->getOpcode() != ISD::CopyToReg)
51 return;
52
53 unsigned Reg = cast<RegisterSDNode>(Use->getOperand(1))->getReg();
Dan Gohman1e57df32008-02-10 18:45:23 +000054 if (TargetRegisterInfo::isVirtualRegister(Reg))
Evan Cheng93f143e2007-09-25 01:54:36 +000055 return;
56
57 unsigned ResNo = Use->getOperand(2).ResNo;
58 if (Def->isTargetOpcode()) {
Chris Lattner5b930372008-01-07 07:27:27 +000059 const TargetInstrDesc &II = TII->get(Def->getTargetOpcode());
Chris Lattner0c2a4f32008-01-07 03:13:06 +000060 if (ResNo >= II.getNumDefs() &&
61 II.ImplicitDefs[ResNo - II.getNumDefs()] == Reg) {
Evan Cheng93f143e2007-09-25 01:54:36 +000062 PhysReg = Reg;
63 const TargetRegisterClass *RC =
Dan Gohman1e57df32008-02-10 18:45:23 +000064 TRI->getPhysicalRegisterRegClass(Def->getValueType(ResNo), Reg);
Evan Cheng93f143e2007-09-25 01:54:36 +000065 Cost = RC->getCopyCost();
66 }
67 }
68}
69
70SUnit *ScheduleDAG::Clone(SUnit *Old) {
71 SUnit *SU = NewSUnit(Old->Node);
Dan Gohmanb100d802008-03-10 23:48:14 +000072 SU->FlaggedNodes = Old->FlaggedNodes;
Evan Cheng93f143e2007-09-25 01:54:36 +000073 SU->InstanceNo = SUnitMap[Old->Node].size();
74 SU->Latency = Old->Latency;
75 SU->isTwoAddress = Old->isTwoAddress;
76 SU->isCommutable = Old->isCommutable;
Evan Chengba597da2007-09-28 22:32:30 +000077 SU->hasPhysRegDefs = Old->hasPhysRegDefs;
Evan Cheng93f143e2007-09-25 01:54:36 +000078 SUnitMap[Old->Node].push_back(SU);
79 return SU;
80}
81
Evan Chengdd3f8b92007-10-05 01:39:18 +000082
Dan Gohmanf17a25c2007-07-18 16:29:46 +000083/// BuildSchedUnits - Build SUnits from the selection dag that we are input.
84/// This SUnit graph is similar to the SelectionDAG, but represents flagged
85/// together nodes with a single SUnit.
86void ScheduleDAG::BuildSchedUnits() {
87 // Reserve entries in the vector for each of the SUnits we are creating. This
88 // ensure that reallocation of the vector won't happen, so SUnit*'s won't get
89 // invalidated.
90 SUnits.reserve(std::distance(DAG.allnodes_begin(), DAG.allnodes_end()));
91
Dan Gohmanf17a25c2007-07-18 16:29:46 +000092 for (SelectionDAG::allnodes_iterator NI = DAG.allnodes_begin(),
93 E = DAG.allnodes_end(); NI != E; ++NI) {
94 if (isPassiveNode(NI)) // Leaf node, e.g. a TargetImmediate.
95 continue;
96
97 // If this node has already been processed, stop now.
Evan Cheng93f143e2007-09-25 01:54:36 +000098 if (SUnitMap[NI].size()) continue;
Dan Gohmanf17a25c2007-07-18 16:29:46 +000099
100 SUnit *NodeSUnit = NewSUnit(NI);
101
102 // See if anything is flagged to this node, if so, add them to flagged
103 // nodes. Nodes can have at most one flag input and one flag output. Flags
104 // are required the be the last operand and result of a node.
105
106 // Scan up, adding flagged preds to FlaggedNodes.
107 SDNode *N = NI;
108 if (N->getNumOperands() &&
109 N->getOperand(N->getNumOperands()-1).getValueType() == MVT::Flag) {
110 do {
111 N = N->getOperand(N->getNumOperands()-1).Val;
112 NodeSUnit->FlaggedNodes.push_back(N);
Evan Cheng93f143e2007-09-25 01:54:36 +0000113 SUnitMap[N].push_back(NodeSUnit);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000114 } while (N->getNumOperands() &&
115 N->getOperand(N->getNumOperands()-1).getValueType()== MVT::Flag);
116 std::reverse(NodeSUnit->FlaggedNodes.begin(),
117 NodeSUnit->FlaggedNodes.end());
118 }
119
120 // Scan down, adding this node and any flagged succs to FlaggedNodes if they
121 // have a user of the flag operand.
122 N = NI;
123 while (N->getValueType(N->getNumValues()-1) == MVT::Flag) {
124 SDOperand FlagVal(N, N->getNumValues()-1);
125
126 // There are either zero or one users of the Flag result.
127 bool HasFlagUse = false;
128 for (SDNode::use_iterator UI = N->use_begin(), E = N->use_end();
129 UI != E; ++UI)
Evan Chengd9387682008-03-04 00:41:45 +0000130 if (FlagVal.isOperandOf(*UI)) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000131 HasFlagUse = true;
132 NodeSUnit->FlaggedNodes.push_back(N);
Evan Cheng93f143e2007-09-25 01:54:36 +0000133 SUnitMap[N].push_back(NodeSUnit);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000134 N = *UI;
135 break;
136 }
137 if (!HasFlagUse) break;
138 }
139
140 // Now all flagged nodes are in FlaggedNodes and N is the bottom-most node.
141 // Update the SUnit
142 NodeSUnit->Node = N;
Evan Cheng93f143e2007-09-25 01:54:36 +0000143 SUnitMap[N].push_back(NodeSUnit);
Evan Chengdd3f8b92007-10-05 01:39:18 +0000144
145 ComputeLatency(NodeSUnit);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000146 }
147
148 // Pass 2: add the preds, succs, etc.
149 for (unsigned su = 0, e = SUnits.size(); su != e; ++su) {
150 SUnit *SU = &SUnits[su];
151 SDNode *MainNode = SU->Node;
152
153 if (MainNode->isTargetOpcode()) {
154 unsigned Opc = MainNode->getTargetOpcode();
Chris Lattner5b930372008-01-07 07:27:27 +0000155 const TargetInstrDesc &TID = TII->get(Opc);
Chris Lattner0c2a4f32008-01-07 03:13:06 +0000156 for (unsigned i = 0; i != TID.getNumOperands(); ++i) {
Evan Cheng93f143e2007-09-25 01:54:36 +0000157 if (TID.getOperandConstraint(i, TOI::TIED_TO) != -1) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000158 SU->isTwoAddress = true;
159 break;
160 }
161 }
Chris Lattnerd8529ab2008-01-07 06:42:05 +0000162 if (TID.isCommutable())
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000163 SU->isCommutable = true;
164 }
165
166 // Find all predecessors and successors of the group.
167 // Temporarily add N to make code simpler.
168 SU->FlaggedNodes.push_back(MainNode);
169
170 for (unsigned n = 0, e = SU->FlaggedNodes.size(); n != e; ++n) {
171 SDNode *N = SU->FlaggedNodes[n];
Evan Chengba597da2007-09-28 22:32:30 +0000172 if (N->isTargetOpcode() &&
Chris Lattner0c2a4f32008-01-07 03:13:06 +0000173 TII->get(N->getTargetOpcode()).getImplicitDefs() &&
174 CountResults(N) > TII->get(N->getTargetOpcode()).getNumDefs())
Evan Chengba597da2007-09-28 22:32:30 +0000175 SU->hasPhysRegDefs = true;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000176
177 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
178 SDNode *OpN = N->getOperand(i).Val;
179 if (isPassiveNode(OpN)) continue; // Not scheduled.
Evan Cheng93f143e2007-09-25 01:54:36 +0000180 SUnit *OpSU = SUnitMap[OpN].front();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000181 assert(OpSU && "Node has no SUnit!");
182 if (OpSU == SU) continue; // In the same group.
183
184 MVT::ValueType OpVT = N->getOperand(i).getValueType();
185 assert(OpVT != MVT::Flag && "Flagged nodes should be in same sunit!");
186 bool isChain = OpVT == MVT::Other;
Evan Cheng93f143e2007-09-25 01:54:36 +0000187
188 unsigned PhysReg = 0;
189 int Cost = 1;
190 // Determine if this is a physical register dependency.
Dan Gohman1e57df32008-02-10 18:45:23 +0000191 CheckForPhysRegDependency(OpN, N, i, TRI, TII, PhysReg, Cost);
Evan Cheng93f143e2007-09-25 01:54:36 +0000192 SU->addPred(OpSU, isChain, false, PhysReg, Cost);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000193 }
194 }
195
196 // Remove MainNode from FlaggedNodes again.
197 SU->FlaggedNodes.pop_back();
198 }
199
200 return;
201}
202
Evan Chengdd3f8b92007-10-05 01:39:18 +0000203void ScheduleDAG::ComputeLatency(SUnit *SU) {
204 const InstrItineraryData &InstrItins = TM.getInstrItineraryData();
205
206 // Compute the latency for the node. We use the sum of the latencies for
207 // all nodes flagged together into this SUnit.
208 if (InstrItins.isEmpty()) {
209 // No latency information.
210 SU->Latency = 1;
211 } else {
212 SU->Latency = 0;
213 if (SU->Node->isTargetOpcode()) {
Chris Lattner3d54fcd2008-01-07 02:46:03 +0000214 unsigned SchedClass =
215 TII->get(SU->Node->getTargetOpcode()).getSchedClass();
Evan Chengdd3f8b92007-10-05 01:39:18 +0000216 InstrStage *S = InstrItins.begin(SchedClass);
217 InstrStage *E = InstrItins.end(SchedClass);
218 for (; S != E; ++S)
219 SU->Latency += S->Cycles;
220 }
221 for (unsigned i = 0, e = SU->FlaggedNodes.size(); i != e; ++i) {
222 SDNode *FNode = SU->FlaggedNodes[i];
223 if (FNode->isTargetOpcode()) {
Chris Lattner3d54fcd2008-01-07 02:46:03 +0000224 unsigned SchedClass =TII->get(FNode->getTargetOpcode()).getSchedClass();
Evan Chengdd3f8b92007-10-05 01:39:18 +0000225 InstrStage *S = InstrItins.begin(SchedClass);
226 InstrStage *E = InstrItins.end(SchedClass);
227 for (; S != E; ++S)
228 SU->Latency += S->Cycles;
229 }
230 }
231 }
232}
233
Roman Levenstein1db9b822008-03-04 11:19:43 +0000234/// CalculateDepths - compute depths using algorithms for the longest
235/// paths in the DAG
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000236void ScheduleDAG::CalculateDepths() {
Roman Levenstein1db9b822008-03-04 11:19:43 +0000237 unsigned DAGSize = SUnits.size();
238 std::vector<unsigned> InDegree(DAGSize);
239 std::vector<SUnit*> WorkList;
240 WorkList.reserve(DAGSize);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000241
Roman Levenstein1db9b822008-03-04 11:19:43 +0000242 // Initialize the data structures
243 for (unsigned i = 0, e = DAGSize; i != e; ++i) {
244 SUnit *SU = &SUnits[i];
245 int NodeNum = SU->NodeNum;
246 unsigned Degree = SU->Preds.size();
247 InDegree[NodeNum] = Degree;
248 SU->Depth = 0;
249
250 // Is it a node without dependencies?
251 if (Degree == 0) {
252 assert(SU->Preds.empty() && "SUnit should have no predecessors");
253 // Collect leaf nodes
254 WorkList.push_back(SU);
255 }
256 }
257
258 // Process nodes in the topological order
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000259 while (!WorkList.empty()) {
Roman Levenstein1db9b822008-03-04 11:19:43 +0000260 SUnit *SU = WorkList.back();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000261 WorkList.pop_back();
Roman Levenstein1db9b822008-03-04 11:19:43 +0000262 unsigned &SUDepth = SU->Depth;
263
264 // Use dynamic programming:
265 // When current node is being processed, all of its dependencies
266 // are already processed.
267 // So, just iterate over all predecessors and take the longest path
268 for (SUnit::const_pred_iterator I = SU->Preds.begin(), E = SU->Preds.end();
269 I != E; ++I) {
270 unsigned PredDepth = I->Dep->Depth;
271 if (PredDepth+1 > SUDepth) {
272 SUDepth = PredDepth + 1;
273 }
274 }
275
276 // Update InDegrees of all nodes depending on current SUnit
277 for (SUnit::const_succ_iterator I = SU->Succs.begin(), E = SU->Succs.end();
278 I != E; ++I) {
279 SUnit *SU = I->Dep;
280 if (!--InDegree[SU->NodeNum])
281 // If all dependencies of the node are processed already,
282 // then the longest path for the node can be computed now
283 WorkList.push_back(SU);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000284 }
285 }
286}
287
Roman Levenstein1db9b822008-03-04 11:19:43 +0000288/// CalculateHeights - compute heights using algorithms for the longest
289/// paths in the DAG
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000290void ScheduleDAG::CalculateHeights() {
Roman Levenstein1db9b822008-03-04 11:19:43 +0000291 unsigned DAGSize = SUnits.size();
292 std::vector<unsigned> InDegree(DAGSize);
293 std::vector<SUnit*> WorkList;
294 WorkList.reserve(DAGSize);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000295
Roman Levenstein1db9b822008-03-04 11:19:43 +0000296 // Initialize the data structures
297 for (unsigned i = 0, e = DAGSize; i != e; ++i) {
298 SUnit *SU = &SUnits[i];
299 int NodeNum = SU->NodeNum;
300 unsigned Degree = SU->Succs.size();
301 InDegree[NodeNum] = Degree;
302 SU->Height = 0;
303
304 // Is it a node without dependencies?
305 if (Degree == 0) {
306 assert(SU->Succs.empty() && "Something wrong");
307 assert(WorkList.empty() && "Should be empty");
308 // Collect leaf nodes
309 WorkList.push_back(SU);
310 }
311 }
312
313 // Process nodes in the topological order
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000314 while (!WorkList.empty()) {
Roman Levenstein1db9b822008-03-04 11:19:43 +0000315 SUnit *SU = WorkList.back();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000316 WorkList.pop_back();
Roman Levenstein1db9b822008-03-04 11:19:43 +0000317 unsigned &SUHeight = SU->Height;
318
319 // Use dynamic programming:
320 // When current node is being processed, all of its dependencies
321 // are already processed.
322 // So, just iterate over all successors and take the longest path
323 for (SUnit::const_succ_iterator I = SU->Succs.begin(), E = SU->Succs.end();
324 I != E; ++I) {
325 unsigned SuccHeight = I->Dep->Height;
326 if (SuccHeight+1 > SUHeight) {
327 SUHeight = SuccHeight + 1;
328 }
329 }
330
331 // Update InDegrees of all nodes depending on current SUnit
332 for (SUnit::const_pred_iterator I = SU->Preds.begin(), E = SU->Preds.end();
333 I != E; ++I) {
334 SUnit *SU = I->Dep;
335 if (!--InDegree[SU->NodeNum])
336 // If all dependencies of the node are processed already,
337 // then the longest path for the node can be computed now
338 WorkList.push_back(SU);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000339 }
340 }
341}
342
343/// CountResults - The results of target nodes have register or immediate
344/// operands first, then an optional chain, and optional flag operands (which do
Dan Gohman0256f1e2008-02-11 19:00:03 +0000345/// not go into the resulting MachineInstr).
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000346unsigned ScheduleDAG::CountResults(SDNode *Node) {
347 unsigned N = Node->getNumValues();
348 while (N && Node->getValueType(N - 1) == MVT::Flag)
349 --N;
350 if (N && Node->getValueType(N - 1) == MVT::Other)
351 --N; // Skip over chain result.
352 return N;
353}
354
Dan Gohman12a9c082008-02-06 22:27:42 +0000355/// CountOperands - The inputs to target nodes have any actual inputs first,
Dan Gohmance256462008-02-16 00:36:48 +0000356/// followed by special operands that describe memory references, then an
357/// optional chain operand, then flag operands. Compute the number of
358/// actual operands that will go into the resulting MachineInstr.
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000359unsigned ScheduleDAG::CountOperands(SDNode *Node) {
Dan Gohmance256462008-02-16 00:36:48 +0000360 unsigned N = ComputeMemOperandsEnd(Node);
Dan Gohman206208c2008-02-11 19:00:34 +0000361 while (N && isa<MemOperandSDNode>(Node->getOperand(N - 1).Val))
Dan Gohman12a9c082008-02-06 22:27:42 +0000362 --N; // Ignore MemOperand nodes
363 return N;
364}
365
Dan Gohmance256462008-02-16 00:36:48 +0000366/// ComputeMemOperandsEnd - Find the index one past the last MemOperandSDNode
367/// operand
368unsigned ScheduleDAG::ComputeMemOperandsEnd(SDNode *Node) {
Dan Gohman12a9c082008-02-06 22:27:42 +0000369 unsigned N = Node->getNumOperands();
370 while (N && Node->getOperand(N - 1).getValueType() == MVT::Flag)
371 --N;
372 if (N && Node->getOperand(N - 1).getValueType() == MVT::Other)
373 --N; // Ignore chain if it exists.
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000374 return N;
375}
376
377static const TargetRegisterClass *getInstrOperandRegClass(
Dan Gohman1e57df32008-02-10 18:45:23 +0000378 const TargetRegisterInfo *TRI,
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000379 const TargetInstrInfo *TII,
Chris Lattner5b930372008-01-07 07:27:27 +0000380 const TargetInstrDesc &II,
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000381 unsigned Op) {
Chris Lattner5b930372008-01-07 07:27:27 +0000382 if (Op >= II.getNumOperands()) {
383 assert(II.isVariadic() && "Invalid operand # of instruction");
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000384 return NULL;
385 }
Chris Lattner5b930372008-01-07 07:27:27 +0000386 if (II.OpInfo[Op].isLookupPtrRegClass())
Chris Lattnereeedb482008-01-07 02:39:19 +0000387 return TII->getPointerRegClass();
Dan Gohman1e57df32008-02-10 18:45:23 +0000388 return TRI->getRegClass(II.OpInfo[Op].RegClass);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000389}
390
Evan Cheng93f143e2007-09-25 01:54:36 +0000391void ScheduleDAG::EmitCopyFromReg(SDNode *Node, unsigned ResNo,
392 unsigned InstanceNo, unsigned SrcReg,
Evan Cheng26639782007-08-02 00:28:15 +0000393 DenseMap<SDOperand, unsigned> &VRBaseMap) {
394 unsigned VRBase = 0;
Dan Gohman1e57df32008-02-10 18:45:23 +0000395 if (TargetRegisterInfo::isVirtualRegister(SrcReg)) {
Evan Cheng26639782007-08-02 00:28:15 +0000396 // Just use the input register directly!
Evan Cheng93f143e2007-09-25 01:54:36 +0000397 if (InstanceNo > 0)
398 VRBaseMap.erase(SDOperand(Node, ResNo));
Evan Cheng26639782007-08-02 00:28:15 +0000399 bool isNew = VRBaseMap.insert(std::make_pair(SDOperand(Node,ResNo),SrcReg));
400 assert(isNew && "Node emitted out of order - early");
401 return;
402 }
403
404 // If the node is only used by a CopyToReg and the dest reg is a vreg, use
405 // the CopyToReg'd destination register instead of creating a new vreg.
Evan Cheng93f143e2007-09-25 01:54:36 +0000406 bool MatchReg = true;
Evan Cheng26639782007-08-02 00:28:15 +0000407 for (SDNode::use_iterator UI = Node->use_begin(), E = Node->use_end();
408 UI != E; ++UI) {
409 SDNode *Use = *UI;
Evan Cheng93f143e2007-09-25 01:54:36 +0000410 bool Match = true;
Evan Cheng26639782007-08-02 00:28:15 +0000411 if (Use->getOpcode() == ISD::CopyToReg &&
412 Use->getOperand(2).Val == Node &&
413 Use->getOperand(2).ResNo == ResNo) {
414 unsigned DestReg = cast<RegisterSDNode>(Use->getOperand(1))->getReg();
Dan Gohman1e57df32008-02-10 18:45:23 +0000415 if (TargetRegisterInfo::isVirtualRegister(DestReg)) {
Evan Cheng26639782007-08-02 00:28:15 +0000416 VRBase = DestReg;
Evan Cheng93f143e2007-09-25 01:54:36 +0000417 Match = false;
418 } else if (DestReg != SrcReg)
419 Match = false;
420 } else {
421 for (unsigned i = 0, e = Use->getNumOperands(); i != e; ++i) {
422 SDOperand Op = Use->getOperand(i);
Evan Cheng4f0345c2007-12-14 08:25:15 +0000423 if (Op.Val != Node || Op.ResNo != ResNo)
Evan Cheng93f143e2007-09-25 01:54:36 +0000424 continue;
425 MVT::ValueType VT = Node->getValueType(Op.ResNo);
426 if (VT != MVT::Other && VT != MVT::Flag)
427 Match = false;
Evan Cheng26639782007-08-02 00:28:15 +0000428 }
429 }
Evan Cheng93f143e2007-09-25 01:54:36 +0000430 MatchReg &= Match;
431 if (VRBase)
432 break;
Evan Cheng26639782007-08-02 00:28:15 +0000433 }
434
Chris Lattnere6fdb062008-03-09 08:49:15 +0000435 const TargetRegisterClass *SrcRC = 0, *DstRC = 0;
436 SrcRC = TRI->getPhysicalRegisterRegClass(Node->getValueType(ResNo), SrcReg);
437
Evan Cheng93f143e2007-09-25 01:54:36 +0000438 // Figure out the register class to create for the destreg.
Chris Lattnere6fdb062008-03-09 08:49:15 +0000439 if (VRBase) {
440 DstRC = RegInfo.getRegClass(VRBase);
441 } else {
442 DstRC = DAG.getTargetLoweringInfo()
443 .getRegClassFor(Node->getValueType(ResNo));
444 }
Evan Cheng93f143e2007-09-25 01:54:36 +0000445
446 // If all uses are reading from the src physical register and copying the
447 // register is either impossible or very expensive, then don't create a copy.
Chris Lattnere6fdb062008-03-09 08:49:15 +0000448 if (MatchReg && SrcRC->getCopyCost() < 0) {
Evan Cheng93f143e2007-09-25 01:54:36 +0000449 VRBase = SrcReg;
450 } else {
Evan Cheng26639782007-08-02 00:28:15 +0000451 // Create the reg, emit the copy.
Chris Lattnere6fdb062008-03-09 08:49:15 +0000452 VRBase = RegInfo.createVirtualRegister(DstRC);
453 TII->copyRegToReg(*BB, BB->end(), VRBase, SrcReg, DstRC, SrcRC);
Evan Cheng26639782007-08-02 00:28:15 +0000454 }
Evan Cheng26639782007-08-02 00:28:15 +0000455
Evan Cheng93f143e2007-09-25 01:54:36 +0000456 if (InstanceNo > 0)
457 VRBaseMap.erase(SDOperand(Node, ResNo));
Evan Cheng26639782007-08-02 00:28:15 +0000458 bool isNew = VRBaseMap.insert(std::make_pair(SDOperand(Node,ResNo), VRBase));
459 assert(isNew && "Node emitted out of order - early");
460}
461
462void ScheduleDAG::CreateVirtualRegisters(SDNode *Node,
463 MachineInstr *MI,
Chris Lattner5b930372008-01-07 07:27:27 +0000464 const TargetInstrDesc &II,
Evan Cheng26639782007-08-02 00:28:15 +0000465 DenseMap<SDOperand, unsigned> &VRBaseMap) {
Chris Lattner0c2a4f32008-01-07 03:13:06 +0000466 for (unsigned i = 0; i < II.getNumDefs(); ++i) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000467 // If the specific node value is only used by a CopyToReg and the dest reg
468 // is a vreg, use the CopyToReg'd destination register instead of creating
469 // a new vreg.
470 unsigned VRBase = 0;
471 for (SDNode::use_iterator UI = Node->use_begin(), E = Node->use_end();
472 UI != E; ++UI) {
473 SDNode *Use = *UI;
474 if (Use->getOpcode() == ISD::CopyToReg &&
475 Use->getOperand(2).Val == Node &&
476 Use->getOperand(2).ResNo == i) {
477 unsigned Reg = cast<RegisterSDNode>(Use->getOperand(1))->getReg();
Dan Gohman1e57df32008-02-10 18:45:23 +0000478 if (TargetRegisterInfo::isVirtualRegister(Reg)) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000479 VRBase = Reg;
Chris Lattner63ab1f22007-12-30 00:41:17 +0000480 MI->addOperand(MachineOperand::CreateReg(Reg, true));
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000481 break;
482 }
483 }
484 }
485
Evan Cheng26639782007-08-02 00:28:15 +0000486 // Create the result registers for this node and add the result regs to
487 // the machine instruction.
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000488 if (VRBase == 0) {
Dan Gohman1e57df32008-02-10 18:45:23 +0000489 const TargetRegisterClass *RC = getInstrOperandRegClass(TRI, TII, II, i);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000490 assert(RC && "Isn't a register operand!");
Chris Lattner1b989192007-12-31 04:13:23 +0000491 VRBase = RegInfo.createVirtualRegister(RC);
Chris Lattner63ab1f22007-12-30 00:41:17 +0000492 MI->addOperand(MachineOperand::CreateReg(VRBase, true));
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000493 }
494
495 bool isNew = VRBaseMap.insert(std::make_pair(SDOperand(Node,i), VRBase));
496 assert(isNew && "Node emitted out of order - early");
497 }
498}
499
500/// getVR - Return the virtual register corresponding to the specified result
501/// of the specified node.
502static unsigned getVR(SDOperand Op, DenseMap<SDOperand, unsigned> &VRBaseMap) {
503 DenseMap<SDOperand, unsigned>::iterator I = VRBaseMap.find(Op);
504 assert(I != VRBaseMap.end() && "Node emitted out of order - late");
505 return I->second;
506}
507
508
509/// AddOperand - Add the specified operand to the specified machine instr. II
510/// specifies the instruction information for the node, and IIOpNum is the
511/// operand number (in the II) that we are adding. IIOpNum and II are used for
512/// assertions only.
513void ScheduleDAG::AddOperand(MachineInstr *MI, SDOperand Op,
514 unsigned IIOpNum,
Chris Lattner5b930372008-01-07 07:27:27 +0000515 const TargetInstrDesc *II,
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000516 DenseMap<SDOperand, unsigned> &VRBaseMap) {
517 if (Op.isTargetOpcode()) {
518 // Note that this case is redundant with the final else block, but we
519 // include it because it is the most common and it makes the logic
520 // simpler here.
521 assert(Op.getValueType() != MVT::Other &&
522 Op.getValueType() != MVT::Flag &&
523 "Chain and flag operands should occur at end of operand list!");
524
525 // Get/emit the operand.
526 unsigned VReg = getVR(Op, VRBaseMap);
Chris Lattner5b930372008-01-07 07:27:27 +0000527 const TargetInstrDesc &TID = MI->getDesc();
528 bool isOptDef = (IIOpNum < TID.getNumOperands())
529 ? (TID.OpInfo[IIOpNum].isOptionalDef()) : false;
Chris Lattner63ab1f22007-12-30 00:41:17 +0000530 MI->addOperand(MachineOperand::CreateReg(VReg, isOptDef));
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000531
532 // Verify that it is right.
Dan Gohman1e57df32008-02-10 18:45:23 +0000533 assert(TargetRegisterInfo::isVirtualRegister(VReg) && "Not a vreg?");
Chris Lattnerc3d37ab2008-03-11 00:59:28 +0000534#ifndef NDEBUG
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000535 if (II) {
Chris Lattnerc3d37ab2008-03-11 00:59:28 +0000536 // There may be no register class for this operand if it is a variadic
537 // argument (RC will be NULL in this case). In this case, we just assume
538 // the regclass is ok.
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000539 const TargetRegisterClass *RC =
Dan Gohman1e57df32008-02-10 18:45:23 +0000540 getInstrOperandRegClass(TRI, TII, *II, IIOpNum);
Chris Lattner1b989192007-12-31 04:13:23 +0000541 const TargetRegisterClass *VRC = RegInfo.getRegClass(VReg);
Chris Lattnerc3d37ab2008-03-11 00:59:28 +0000542 if (RC && VRC != RC) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000543 cerr << "Register class of operand and regclass of use don't agree!\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000544 cerr << "Operand = " << IIOpNum << "\n";
545 cerr << "Op->Val = "; Op.Val->dump(&DAG); cerr << "\n";
546 cerr << "MI = "; MI->print(cerr);
547 cerr << "VReg = " << VReg << "\n";
548 cerr << "VReg RegClass size = " << VRC->getSize()
549 << ", align = " << VRC->getAlignment() << "\n";
550 cerr << "Expected RegClass size = " << RC->getSize()
551 << ", align = " << RC->getAlignment() << "\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000552 cerr << "Fatal error, aborting.\n";
553 abort();
554 }
555 }
Chris Lattnerc3d37ab2008-03-11 00:59:28 +0000556#endif
Chris Lattner8dfd3122007-12-30 00:51:11 +0000557 } else if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Chris Lattner63ab1f22007-12-30 00:41:17 +0000558 MI->addOperand(MachineOperand::CreateImm(C->getValue()));
Nate Begemane2ba64f2008-02-14 08:57:00 +0000559 } else if (ConstantFPSDNode *F = dyn_cast<ConstantFPSDNode>(Op)) {
560 const Type *FType = MVT::getTypeForValueType(Op.getValueType());
561 ConstantFP *CFP = ConstantFP::get(FType, F->getValueAPF());
562 MI->addOperand(MachineOperand::CreateFPImm(CFP));
Chris Lattner8dfd3122007-12-30 00:51:11 +0000563 } else if (RegisterSDNode *R = dyn_cast<RegisterSDNode>(Op)) {
Chris Lattner63ab1f22007-12-30 00:41:17 +0000564 MI->addOperand(MachineOperand::CreateReg(R->getReg(), false));
Chris Lattner8dfd3122007-12-30 00:51:11 +0000565 } else if (GlobalAddressSDNode *TGA = dyn_cast<GlobalAddressSDNode>(Op)) {
566 MI->addOperand(MachineOperand::CreateGA(TGA->getGlobal(),TGA->getOffset()));
567 } else if (BasicBlockSDNode *BB = dyn_cast<BasicBlockSDNode>(Op)) {
568 MI->addOperand(MachineOperand::CreateMBB(BB->getBasicBlock()));
569 } else if (FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(Op)) {
570 MI->addOperand(MachineOperand::CreateFI(FI->getIndex()));
571 } else if (JumpTableSDNode *JT = dyn_cast<JumpTableSDNode>(Op)) {
572 MI->addOperand(MachineOperand::CreateJTI(JT->getIndex()));
573 } else if (ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(Op)) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000574 int Offset = CP->getOffset();
575 unsigned Align = CP->getAlignment();
576 const Type *Type = CP->getType();
577 // MachineConstantPool wants an explicit alignment.
578 if (Align == 0) {
579 Align = TM.getTargetData()->getPreferredTypeAlignmentShift(Type);
580 if (Align == 0) {
581 // Alignment of vector types. FIXME!
Duncan Sandsf99fdc62007-11-01 20:53:16 +0000582 Align = TM.getTargetData()->getABITypeSize(Type);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000583 Align = Log2_64(Align);
584 }
585 }
586
587 unsigned Idx;
588 if (CP->isMachineConstantPoolEntry())
589 Idx = ConstPool->getConstantPoolIndex(CP->getMachineCPVal(), Align);
590 else
591 Idx = ConstPool->getConstantPoolIndex(CP->getConstVal(), Align);
Chris Lattner8dfd3122007-12-30 00:51:11 +0000592 MI->addOperand(MachineOperand::CreateCPI(Idx, Offset));
593 } else if (ExternalSymbolSDNode *ES = dyn_cast<ExternalSymbolSDNode>(Op)) {
594 MI->addOperand(MachineOperand::CreateES(ES->getSymbol()));
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000595 } else {
596 assert(Op.getValueType() != MVT::Other &&
597 Op.getValueType() != MVT::Flag &&
598 "Chain and flag operands should occur at end of operand list!");
599 unsigned VReg = getVR(Op, VRBaseMap);
Chris Lattner63ab1f22007-12-30 00:41:17 +0000600 MI->addOperand(MachineOperand::CreateReg(VReg, false));
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000601
Chris Lattnere6fdb062008-03-09 08:49:15 +0000602 // Verify that it is right. Note that the reg class of the physreg and the
603 // vreg don't necessarily need to match, but the target copy insertion has
604 // to be able to handle it. This handles things like copies from ST(0) to
605 // an FP vreg on x86.
Dan Gohman1e57df32008-02-10 18:45:23 +0000606 assert(TargetRegisterInfo::isVirtualRegister(VReg) && "Not a vreg?");
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000607 if (II) {
Chris Lattnere6fdb062008-03-09 08:49:15 +0000608 assert(getInstrOperandRegClass(TRI, TII, *II, IIOpNum) &&
609 "Don't have operand info for this instruction!");
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000610 }
611 }
612
613}
614
Dan Gohman12a9c082008-02-06 22:27:42 +0000615void ScheduleDAG::AddMemOperand(MachineInstr *MI, const MemOperand &MO) {
616 MI->addMemOperand(MO);
617}
618
Christopher Lambe95328d2007-07-26 08:12:07 +0000619// Returns the Register Class of a subregister
620static const TargetRegisterClass *getSubRegisterRegClass(
621 const TargetRegisterClass *TRC,
622 unsigned SubIdx) {
623 // Pick the register class of the subregister
Dan Gohman1e57df32008-02-10 18:45:23 +0000624 TargetRegisterInfo::regclass_iterator I =
625 TRC->subregclasses_begin() + SubIdx-1;
Christopher Lambe95328d2007-07-26 08:12:07 +0000626 assert(I < TRC->subregclasses_end() &&
627 "Invalid subregister index for register class");
628 return *I;
629}
630
631static const TargetRegisterClass *getSuperregRegisterClass(
632 const TargetRegisterClass *TRC,
633 unsigned SubIdx,
634 MVT::ValueType VT) {
635 // Pick the register class of the superegister for this type
Dan Gohman1e57df32008-02-10 18:45:23 +0000636 for (TargetRegisterInfo::regclass_iterator I = TRC->superregclasses_begin(),
Christopher Lambe95328d2007-07-26 08:12:07 +0000637 E = TRC->superregclasses_end(); I != E; ++I)
638 if ((*I)->hasType(VT) && getSubRegisterRegClass(*I, SubIdx) == TRC)
639 return *I;
640 assert(false && "Couldn't find the register class");
641 return 0;
642}
643
644/// EmitSubregNode - Generate machine code for subreg nodes.
645///
646void ScheduleDAG::EmitSubregNode(SDNode *Node,
647 DenseMap<SDOperand, unsigned> &VRBaseMap) {
648 unsigned VRBase = 0;
649 unsigned Opc = Node->getTargetOpcode();
650 if (Opc == TargetInstrInfo::EXTRACT_SUBREG) {
651 // If the node is only used by a CopyToReg and the dest reg is a vreg, use
652 // the CopyToReg'd destination register instead of creating a new vreg.
653 for (SDNode::use_iterator UI = Node->use_begin(), E = Node->use_end();
654 UI != E; ++UI) {
655 SDNode *Use = *UI;
656 if (Use->getOpcode() == ISD::CopyToReg &&
657 Use->getOperand(2).Val == Node) {
658 unsigned DestReg = cast<RegisterSDNode>(Use->getOperand(1))->getReg();
Dan Gohman1e57df32008-02-10 18:45:23 +0000659 if (TargetRegisterInfo::isVirtualRegister(DestReg)) {
Christopher Lambe95328d2007-07-26 08:12:07 +0000660 VRBase = DestReg;
661 break;
662 }
663 }
664 }
665
666 unsigned SubIdx = cast<ConstantSDNode>(Node->getOperand(1))->getValue();
667
668 // TODO: If the node is a use of a CopyFromReg from a physical register
669 // fold the extract into the copy now
670
Christopher Lambe95328d2007-07-26 08:12:07 +0000671 // Create the extract_subreg machine instruction.
672 MachineInstr *MI =
673 new MachineInstr(BB, TII->get(TargetInstrInfo::EXTRACT_SUBREG));
674
675 // Figure out the register class to create for the destreg.
676 unsigned VReg = getVR(Node->getOperand(0), VRBaseMap);
Chris Lattner1b989192007-12-31 04:13:23 +0000677 const TargetRegisterClass *TRC = RegInfo.getRegClass(VReg);
Christopher Lambe95328d2007-07-26 08:12:07 +0000678 const TargetRegisterClass *SRC = getSubRegisterRegClass(TRC, SubIdx);
679
680 if (VRBase) {
681 // Grab the destination register
Chris Lattnere6fdb062008-03-09 08:49:15 +0000682 const TargetRegisterClass *DRC = RegInfo.getRegClass(VRBase);
Christopher Lambe08d9ec2008-01-31 07:09:08 +0000683 assert(SRC && DRC && SRC == DRC &&
Christopher Lambe95328d2007-07-26 08:12:07 +0000684 "Source subregister and destination must have the same class");
685 } else {
686 // Create the reg
Christopher Lambe08d9ec2008-01-31 07:09:08 +0000687 assert(SRC && "Couldn't find source register class");
Chris Lattner1b989192007-12-31 04:13:23 +0000688 VRBase = RegInfo.createVirtualRegister(SRC);
Christopher Lambe95328d2007-07-26 08:12:07 +0000689 }
690
691 // Add def, source, and subreg index
Chris Lattner63ab1f22007-12-30 00:41:17 +0000692 MI->addOperand(MachineOperand::CreateReg(VRBase, true));
Christopher Lambe95328d2007-07-26 08:12:07 +0000693 AddOperand(MI, Node->getOperand(0), 0, 0, VRBaseMap);
Chris Lattner8dfd3122007-12-30 00:51:11 +0000694 MI->addOperand(MachineOperand::CreateImm(SubIdx));
Christopher Lambe95328d2007-07-26 08:12:07 +0000695
696 } else if (Opc == TargetInstrInfo::INSERT_SUBREG) {
Evan Chengbd97af02008-03-10 19:31:26 +0000697 assert((Node->getNumOperands() == 2 || Node->getNumOperands() == 3) &&
Christopher Lambe95328d2007-07-26 08:12:07 +0000698 "Malformed insert_subreg node");
Evan Chengbd97af02008-03-10 19:31:26 +0000699 bool isUndefInput = (Node->getNumOperands() == 2);
700 unsigned SubReg = 0;
701 unsigned SubIdx = 0;
702
703 if (isUndefInput) {
704 SubReg = getVR(Node->getOperand(0), VRBaseMap);
705 SubIdx = cast<ConstantSDNode>(Node->getOperand(1))->getValue();
706 } else {
707 SubReg = getVR(Node->getOperand(1), VRBaseMap);
708 SubIdx = cast<ConstantSDNode>(Node->getOperand(2))->getValue();
709 }
Christopher Lambe95328d2007-07-26 08:12:07 +0000710
Chris Lattnerb70e1512007-12-31 04:16:08 +0000711 // TODO: Add tracking info to MachineRegisterInfo of which vregs are subregs
Christopher Lambe95328d2007-07-26 08:12:07 +0000712 // to allow coalescing in the allocator
713
714 // If the node is only used by a CopyToReg and the dest reg is a vreg, use
715 // the CopyToReg'd destination register instead of creating a new vreg.
716 // If the CopyToReg'd destination register is physical, then fold the
717 // insert into the copy
718 for (SDNode::use_iterator UI = Node->use_begin(), E = Node->use_end();
719 UI != E; ++UI) {
720 SDNode *Use = *UI;
721 if (Use->getOpcode() == ISD::CopyToReg &&
722 Use->getOperand(2).Val == Node) {
723 unsigned DestReg = cast<RegisterSDNode>(Use->getOperand(1))->getReg();
Dan Gohman1e57df32008-02-10 18:45:23 +0000724 if (TargetRegisterInfo::isVirtualRegister(DestReg)) {
Christopher Lambe95328d2007-07-26 08:12:07 +0000725 VRBase = DestReg;
726 break;
727 }
728 }
729 }
730
731 // Create the insert_subreg machine instruction.
732 MachineInstr *MI =
733 new MachineInstr(BB, TII->get(TargetInstrInfo::INSERT_SUBREG));
734
735 // Figure out the register class to create for the destreg.
736 const TargetRegisterClass *TRC = 0;
737 if (VRBase) {
Chris Lattner1b989192007-12-31 04:13:23 +0000738 TRC = RegInfo.getRegClass(VRBase);
Christopher Lambe95328d2007-07-26 08:12:07 +0000739 } else {
Chris Lattner1b989192007-12-31 04:13:23 +0000740 TRC = getSuperregRegisterClass(RegInfo.getRegClass(SubReg), SubIdx,
Christopher Lambe95328d2007-07-26 08:12:07 +0000741 Node->getValueType(0));
742 assert(TRC && "Couldn't determine register class for insert_subreg");
Chris Lattner1b989192007-12-31 04:13:23 +0000743 VRBase = RegInfo.createVirtualRegister(TRC); // Create the reg
Christopher Lambe95328d2007-07-26 08:12:07 +0000744 }
745
Chris Lattner63ab1f22007-12-30 00:41:17 +0000746 MI->addOperand(MachineOperand::CreateReg(VRBase, true));
Evan Chengbd97af02008-03-10 19:31:26 +0000747 AddOperand(MI, Node->getOperand(0), 0, 0, VRBaseMap);
748 if (!isUndefInput)
749 AddOperand(MI, Node->getOperand(1), 0, 0, VRBaseMap);
Chris Lattner8dfd3122007-12-30 00:51:11 +0000750 MI->addOperand(MachineOperand::CreateImm(SubIdx));
Christopher Lambe95328d2007-07-26 08:12:07 +0000751 } else
752 assert(0 && "Node is not a subreg insert or extract");
753
754 bool isNew = VRBaseMap.insert(std::make_pair(SDOperand(Node,0), VRBase));
755 assert(isNew && "Node emitted out of order - early");
756}
757
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000758/// EmitNode - Generate machine code for an node and needed dependencies.
759///
Evan Cheng93f143e2007-09-25 01:54:36 +0000760void ScheduleDAG::EmitNode(SDNode *Node, unsigned InstanceNo,
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000761 DenseMap<SDOperand, unsigned> &VRBaseMap) {
762 // If machine instruction
763 if (Node->isTargetOpcode()) {
764 unsigned Opc = Node->getTargetOpcode();
Christopher Lambe95328d2007-07-26 08:12:07 +0000765
766 // Handle subreg insert/extract specially
767 if (Opc == TargetInstrInfo::EXTRACT_SUBREG ||
768 Opc == TargetInstrInfo::INSERT_SUBREG) {
769 EmitSubregNode(Node, VRBaseMap);
770 return;
771 }
772
Chris Lattner5b930372008-01-07 07:27:27 +0000773 const TargetInstrDesc &II = TII->get(Opc);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000774
775 unsigned NumResults = CountResults(Node);
776 unsigned NodeOperands = CountOperands(Node);
Dan Gohmance256462008-02-16 00:36:48 +0000777 unsigned MemOperandsEnd = ComputeMemOperandsEnd(Node);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000778 unsigned NumMIOperands = NodeOperands + NumResults;
Chris Lattner0c2a4f32008-01-07 03:13:06 +0000779 bool HasPhysRegOuts = (NumResults > II.getNumDefs()) &&
780 II.getImplicitDefs() != 0;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000781#ifndef NDEBUG
Chris Lattner0c2a4f32008-01-07 03:13:06 +0000782 assert((II.getNumOperands() == NumMIOperands ||
Chris Lattner2fb37c02008-01-07 05:19:29 +0000783 HasPhysRegOuts || II.isVariadic()) &&
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000784 "#operands for dag node doesn't match .td file!");
785#endif
786
787 // Create the new machine instruction.
788 MachineInstr *MI = new MachineInstr(II);
789
790 // Add result register values for things that are defined by this
791 // instruction.
792 if (NumResults)
Evan Cheng26639782007-08-02 00:28:15 +0000793 CreateVirtualRegisters(Node, MI, II, VRBaseMap);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000794
795 // Emit all of the actual operands of this instruction, adding them to the
796 // instruction as appropriate.
797 for (unsigned i = 0; i != NodeOperands; ++i)
Chris Lattner0c2a4f32008-01-07 03:13:06 +0000798 AddOperand(MI, Node->getOperand(i), i+II.getNumDefs(), &II, VRBaseMap);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000799
Dan Gohman12a9c082008-02-06 22:27:42 +0000800 // Emit all of the memory operands of this instruction
Dan Gohmance256462008-02-16 00:36:48 +0000801 for (unsigned i = NodeOperands; i != MemOperandsEnd; ++i)
Dan Gohman12a9c082008-02-06 22:27:42 +0000802 AddMemOperand(MI, cast<MemOperandSDNode>(Node->getOperand(i))->MO);
803
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000804 // Commute node if it has been determined to be profitable.
805 if (CommuteSet.count(Node)) {
806 MachineInstr *NewMI = TII->commuteInstruction(MI);
807 if (NewMI == 0)
808 DOUT << "Sched: COMMUTING FAILED!\n";
809 else {
810 DOUT << "Sched: COMMUTED TO: " << *NewMI;
811 if (MI != NewMI) {
812 delete MI;
813 MI = NewMI;
814 }
Evan Cheng7f6ade32008-02-28 07:40:24 +0000815 ++NumCommutes;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000816 }
817 }
818
Evan Chenga53c40a2008-02-01 09:10:45 +0000819 if (II.usesCustomDAGSchedInsertionHook())
Evan Cheng2d373922008-01-30 19:35:32 +0000820 // Insert this instruction into the basic block using a target
821 // specific inserter which may returns a new basic block.
Evan Chenge637db12008-01-30 18:18:23 +0000822 BB = DAG.getTargetLoweringInfo().EmitInstrWithCustomInserter(MI, BB);
Evan Cheng2d373922008-01-30 19:35:32 +0000823 else
824 BB->push_back(MI);
Evan Cheng26639782007-08-02 00:28:15 +0000825
826 // Additional results must be an physical register def.
827 if (HasPhysRegOuts) {
Chris Lattner0c2a4f32008-01-07 03:13:06 +0000828 for (unsigned i = II.getNumDefs(); i < NumResults; ++i) {
829 unsigned Reg = II.getImplicitDefs()[i - II.getNumDefs()];
Evan Cheng0af04f72007-08-02 05:29:38 +0000830 if (Node->hasAnyUseOfValue(i))
Evan Cheng93f143e2007-09-25 01:54:36 +0000831 EmitCopyFromReg(Node, i, InstanceNo, Reg, VRBaseMap);
Evan Cheng26639782007-08-02 00:28:15 +0000832 }
833 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000834 } else {
835 switch (Node->getOpcode()) {
836 default:
837#ifndef NDEBUG
838 Node->dump(&DAG);
839#endif
840 assert(0 && "This target-independent node should have been selected!");
841 case ISD::EntryToken: // fall thru
842 case ISD::TokenFactor:
843 case ISD::LABEL:
Evan Cheng2e28d622008-02-02 04:07:54 +0000844 case ISD::DECLARE:
Dan Gohman12a9c082008-02-06 22:27:42 +0000845 case ISD::SRCVALUE:
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000846 break;
847 case ISD::CopyToReg: {
Chris Lattner0d128722008-03-09 09:15:31 +0000848 unsigned SrcReg;
849 SDOperand SrcVal = Node->getOperand(2);
850 if (RegisterSDNode *R = dyn_cast<RegisterSDNode>(SrcVal))
851 SrcReg = R->getReg();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000852 else
Chris Lattner0d128722008-03-09 09:15:31 +0000853 SrcReg = getVR(SrcVal, VRBaseMap);
854
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000855 unsigned DestReg = cast<RegisterSDNode>(Node->getOperand(1))->getReg();
Chris Lattner0d128722008-03-09 09:15:31 +0000856 if (SrcReg == DestReg) // Coalesced away the copy? Ignore.
857 break;
858
859 const TargetRegisterClass *SrcTRC = 0, *DstTRC = 0;
860 // Get the register classes of the src/dst.
861 if (TargetRegisterInfo::isVirtualRegister(SrcReg))
862 SrcTRC = RegInfo.getRegClass(SrcReg);
863 else
864 SrcTRC = TRI->getPhysicalRegisterRegClass(SrcVal.getValueType(),SrcReg);
865
866 if (TargetRegisterInfo::isVirtualRegister(DestReg))
867 DstTRC = RegInfo.getRegClass(DestReg);
868 else
869 DstTRC = TRI->getPhysicalRegisterRegClass(
870 Node->getOperand(1).getValueType(),
871 DestReg);
872 TII->copyRegToReg(*BB, BB->end(), DestReg, SrcReg, DstTRC, SrcTRC);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000873 break;
874 }
875 case ISD::CopyFromReg: {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000876 unsigned SrcReg = cast<RegisterSDNode>(Node->getOperand(1))->getReg();
Evan Cheng93f143e2007-09-25 01:54:36 +0000877 EmitCopyFromReg(Node, 0, InstanceNo, SrcReg, VRBaseMap);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000878 break;
879 }
880 case ISD::INLINEASM: {
881 unsigned NumOps = Node->getNumOperands();
882 if (Node->getOperand(NumOps-1).getValueType() == MVT::Flag)
883 --NumOps; // Ignore the flag operand.
884
885 // Create the inline asm machine instruction.
886 MachineInstr *MI =
887 new MachineInstr(BB, TII->get(TargetInstrInfo::INLINEASM));
888
889 // Add the asm string as an external symbol operand.
890 const char *AsmStr =
891 cast<ExternalSymbolSDNode>(Node->getOperand(1))->getSymbol();
Chris Lattner8dfd3122007-12-30 00:51:11 +0000892 MI->addOperand(MachineOperand::CreateES(AsmStr));
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000893
894 // Add all of the operand registers to the instruction.
895 for (unsigned i = 2; i != NumOps;) {
896 unsigned Flags = cast<ConstantSDNode>(Node->getOperand(i))->getValue();
897 unsigned NumVals = Flags >> 3;
898
Chris Lattner8dfd3122007-12-30 00:51:11 +0000899 MI->addOperand(MachineOperand::CreateImm(Flags));
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000900 ++i; // Skip the ID value.
901
902 switch (Flags & 7) {
903 default: assert(0 && "Bad flags!");
904 case 1: // Use of register.
905 for (; NumVals; --NumVals, ++i) {
906 unsigned Reg = cast<RegisterSDNode>(Node->getOperand(i))->getReg();
Chris Lattner63ab1f22007-12-30 00:41:17 +0000907 MI->addOperand(MachineOperand::CreateReg(Reg, false));
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000908 }
909 break;
910 case 2: // Def of register.
911 for (; NumVals; --NumVals, ++i) {
912 unsigned Reg = cast<RegisterSDNode>(Node->getOperand(i))->getReg();
Chris Lattner63ab1f22007-12-30 00:41:17 +0000913 MI->addOperand(MachineOperand::CreateReg(Reg, true));
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000914 }
915 break;
916 case 3: { // Immediate.
Chris Lattner23544c12007-08-25 00:53:07 +0000917 for (; NumVals; --NumVals, ++i) {
918 if (ConstantSDNode *CS =
919 dyn_cast<ConstantSDNode>(Node->getOperand(i))) {
Chris Lattner63ab1f22007-12-30 00:41:17 +0000920 MI->addOperand(MachineOperand::CreateImm(CS->getValue()));
Dale Johannesencfb19e62007-11-05 21:20:28 +0000921 } else if (GlobalAddressSDNode *GA =
922 dyn_cast<GlobalAddressSDNode>(Node->getOperand(i))) {
Chris Lattner8dfd3122007-12-30 00:51:11 +0000923 MI->addOperand(MachineOperand::CreateGA(GA->getGlobal(),
924 GA->getOffset()));
Dale Johannesencfb19e62007-11-05 21:20:28 +0000925 } else {
Chris Lattner8dfd3122007-12-30 00:51:11 +0000926 BasicBlockSDNode *BB =cast<BasicBlockSDNode>(Node->getOperand(i));
927 MI->addOperand(MachineOperand::CreateMBB(BB->getBasicBlock()));
Chris Lattner23544c12007-08-25 00:53:07 +0000928 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000929 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000930 break;
931 }
932 case 4: // Addressing mode.
933 // The addressing mode has been selected, just add all of the
934 // operands to the machine instruction.
935 for (; NumVals; --NumVals, ++i)
936 AddOperand(MI, Node->getOperand(i), 0, 0, VRBaseMap);
937 break;
938 }
939 }
940 break;
941 }
942 }
943 }
944}
945
946void ScheduleDAG::EmitNoop() {
947 TII->insertNoop(*BB, BB->end());
948}
949
Chris Lattner4e15fcc2008-03-09 07:51:01 +0000950void ScheduleDAG::EmitCrossRCCopy(SUnit *SU,
951 DenseMap<SUnit*, unsigned> &VRBaseMap) {
Evan Cheng5ec4b762007-09-26 21:36:17 +0000952 for (SUnit::const_pred_iterator I = SU->Preds.begin(), E = SU->Preds.end();
953 I != E; ++I) {
954 if (I->isCtrl) continue; // ignore chain preds
955 if (!I->Dep->Node) {
956 // Copy to physical register.
957 DenseMap<SUnit*, unsigned>::iterator VRI = VRBaseMap.find(I->Dep);
958 assert(VRI != VRBaseMap.end() && "Node emitted out of order - late");
959 // Find the destination physical register.
960 unsigned Reg = 0;
961 for (SUnit::const_succ_iterator II = SU->Succs.begin(),
962 EE = SU->Succs.end(); II != EE; ++II) {
963 if (I->Reg) {
964 Reg = I->Reg;
965 break;
966 }
967 }
968 assert(I->Reg && "Unknown physical register!");
Owen Anderson8f2c8932007-12-31 06:32:00 +0000969 TII->copyRegToReg(*BB, BB->end(), Reg, VRI->second,
Evan Cheng5ec4b762007-09-26 21:36:17 +0000970 SU->CopyDstRC, SU->CopySrcRC);
971 } else {
972 // Copy from physical register.
973 assert(I->Reg && "Unknown physical register!");
Chris Lattner1b989192007-12-31 04:13:23 +0000974 unsigned VRBase = RegInfo.createVirtualRegister(SU->CopyDstRC);
Evan Cheng5ec4b762007-09-26 21:36:17 +0000975 bool isNew = VRBaseMap.insert(std::make_pair(SU, VRBase));
976 assert(isNew && "Node emitted out of order - early");
Owen Anderson8f2c8932007-12-31 06:32:00 +0000977 TII->copyRegToReg(*BB, BB->end(), VRBase, I->Reg,
Evan Cheng5ec4b762007-09-26 21:36:17 +0000978 SU->CopyDstRC, SU->CopySrcRC);
979 }
980 break;
981 }
982}
983
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000984/// EmitSchedule - Emit the machine code in scheduled order.
985void ScheduleDAG::EmitSchedule() {
986 // If this is the first basic block in the function, and if it has live ins
987 // that need to be copied into vregs, emit the copies into the top of the
988 // block before emitting the code for the block.
Evan Cheng2d373922008-01-30 19:35:32 +0000989 if (&MF->front() == BB) {
Chris Lattner1b989192007-12-31 04:13:23 +0000990 for (MachineRegisterInfo::livein_iterator LI = RegInfo.livein_begin(),
991 E = RegInfo.livein_end(); LI != E; ++LI)
Evan Chengb3d91cf2007-09-26 06:25:56 +0000992 if (LI->second) {
Chris Lattner1b989192007-12-31 04:13:23 +0000993 const TargetRegisterClass *RC = RegInfo.getRegClass(LI->second);
Evan Cheng2d373922008-01-30 19:35:32 +0000994 TII->copyRegToReg(*MF->begin(), MF->begin()->end(), LI->second,
Evan Chengb3d91cf2007-09-26 06:25:56 +0000995 LI->first, RC, RC);
996 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000997 }
998
999
1000 // Finally, emit the code for all of the scheduled instructions.
1001 DenseMap<SDOperand, unsigned> VRBaseMap;
Evan Cheng5ec4b762007-09-26 21:36:17 +00001002 DenseMap<SUnit*, unsigned> CopyVRBaseMap;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001003 for (unsigned i = 0, e = Sequence.size(); i != e; i++) {
1004 if (SUnit *SU = Sequence[i]) {
Evan Cheng93f143e2007-09-25 01:54:36 +00001005 for (unsigned j = 0, ee = SU->FlaggedNodes.size(); j != ee; ++j)
1006 EmitNode(SU->FlaggedNodes[j], SU->InstanceNo, VRBaseMap);
Evan Cheng5ec4b762007-09-26 21:36:17 +00001007 if (SU->Node)
1008 EmitNode(SU->Node, SU->InstanceNo, VRBaseMap);
1009 else
1010 EmitCrossRCCopy(SU, CopyVRBaseMap);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001011 } else {
1012 // Null SUnit* is a noop.
1013 EmitNoop();
1014 }
1015 }
1016}
1017
1018/// dump - dump the schedule.
1019void ScheduleDAG::dumpSchedule() const {
1020 for (unsigned i = 0, e = Sequence.size(); i != e; i++) {
1021 if (SUnit *SU = Sequence[i])
1022 SU->dump(&DAG);
1023 else
1024 cerr << "**** NOOP ****\n";
1025 }
1026}
1027
1028
1029/// Run - perform scheduling.
1030///
1031MachineBasicBlock *ScheduleDAG::Run() {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001032 Schedule();
1033 return BB;
1034}
1035
1036/// SUnit - Scheduling unit. It's an wrapper around either a single SDNode or
1037/// a group of nodes flagged together.
1038void SUnit::dump(const SelectionDAG *G) const {
1039 cerr << "SU(" << NodeNum << "): ";
Evan Cheng5ec4b762007-09-26 21:36:17 +00001040 if (Node)
1041 Node->dump(G);
1042 else
1043 cerr << "CROSS RC COPY ";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001044 cerr << "\n";
1045 if (FlaggedNodes.size() != 0) {
1046 for (unsigned i = 0, e = FlaggedNodes.size(); i != e; i++) {
1047 cerr << " ";
1048 FlaggedNodes[i]->dump(G);
1049 cerr << "\n";
1050 }
1051 }
1052}
1053
1054void SUnit::dumpAll(const SelectionDAG *G) const {
1055 dump(G);
1056
1057 cerr << " # preds left : " << NumPredsLeft << "\n";
1058 cerr << " # succs left : " << NumSuccsLeft << "\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001059 cerr << " Latency : " << Latency << "\n";
1060 cerr << " Depth : " << Depth << "\n";
1061 cerr << " Height : " << Height << "\n";
1062
1063 if (Preds.size() != 0) {
1064 cerr << " Predecessors:\n";
1065 for (SUnit::const_succ_iterator I = Preds.begin(), E = Preds.end();
1066 I != E; ++I) {
Evan Chenge7959472007-09-19 01:38:40 +00001067 if (I->isCtrl)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001068 cerr << " ch #";
1069 else
1070 cerr << " val #";
Evan Cheng93f143e2007-09-25 01:54:36 +00001071 cerr << I->Dep << " - SU(" << I->Dep->NodeNum << ")";
1072 if (I->isSpecial)
1073 cerr << " *";
1074 cerr << "\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001075 }
1076 }
1077 if (Succs.size() != 0) {
1078 cerr << " Successors:\n";
1079 for (SUnit::const_succ_iterator I = Succs.begin(), E = Succs.end();
1080 I != E; ++I) {
Evan Chenge7959472007-09-19 01:38:40 +00001081 if (I->isCtrl)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001082 cerr << " ch #";
1083 else
1084 cerr << " val #";
Evan Cheng93f143e2007-09-25 01:54:36 +00001085 cerr << I->Dep << " - SU(" << I->Dep->NodeNum << ")";
1086 if (I->isSpecial)
1087 cerr << " *";
1088 cerr << "\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001089 }
1090 }
1091 cerr << "\n";
1092}