blob: 8a32fee1eb538af8b6cd65eb71c28c586c3d99bb [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//
Jim Laskey5a608dd2005-10-31 12:49:09 +00005// This file was developed by James M. Laskey and is distributed under the
Chris Lattnerd32b2362005-08-18 18:45:24 +00006// University of Illinois Open Source License. See LICENSE.TXT for details.
7//
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
Evan Chenge165a782006-05-11 23:55:42 +000016#define DEBUG_TYPE "sched"
Chris Lattnerb0d21ef2006-03-08 04:25:59 +000017#include "llvm/CodeGen/ScheduleDAG.h"
Chris Lattner5839bf22005-08-26 17:15:30 +000018#include "llvm/CodeGen/MachineConstantPool.h"
Chris Lattner4ccd4062005-08-19 20:45:43 +000019#include "llvm/CodeGen/MachineFunction.h"
Chris Lattner4ccd4062005-08-19 20:45:43 +000020#include "llvm/CodeGen/SSARegMap.h"
Chris Lattner2d973e42005-08-18 20:07:59 +000021#include "llvm/Target/TargetMachine.h"
22#include "llvm/Target/TargetInstrInfo.h"
Chris Lattner025c39b2005-08-26 20:54:47 +000023#include "llvm/Target/TargetLowering.h"
Evan Chenge165a782006-05-11 23:55:42 +000024#include "llvm/Support/Debug.h"
Chris Lattner54a30b92006-03-20 01:51:46 +000025#include "llvm/Support/MathExtras.h"
Evan Chenge165a782006-05-11 23:55:42 +000026#include <iostream>
Chris Lattnerd32b2362005-08-18 18:45:24 +000027using namespace llvm;
28
Jim Laskeye6b90fb2005-09-26 21:57:04 +000029
Evan Chenge165a782006-05-11 23:55:42 +000030/// BuildSchedUnits - Build SUnits from the selection dag that we are input.
31/// This SUnit graph is similar to the SelectionDAG, but represents flagged
32/// together nodes with a single SUnit.
33void ScheduleDAG::BuildSchedUnits() {
34 // Reserve entries in the vector for each of the SUnits we are creating. This
35 // ensure that reallocation of the vector won't happen, so SUnit*'s won't get
36 // invalidated.
37 SUnits.reserve(std::distance(DAG.allnodes_begin(), DAG.allnodes_end()));
38
39 const InstrItineraryData &InstrItins = TM.getInstrItineraryData();
40
41 for (SelectionDAG::allnodes_iterator NI = DAG.allnodes_begin(),
42 E = DAG.allnodes_end(); NI != E; ++NI) {
43 if (isPassiveNode(NI)) // Leaf node, e.g. a TargetImmediate.
44 continue;
45
46 // If this node has already been processed, stop now.
47 if (SUnitMap[NI]) continue;
48
49 SUnit *NodeSUnit = NewSUnit(NI);
50
51 // See if anything is flagged to this node, if so, add them to flagged
52 // nodes. Nodes can have at most one flag input and one flag output. Flags
53 // are required the be the last operand and result of a node.
54
55 // Scan up, adding flagged preds to FlaggedNodes.
56 SDNode *N = NI;
57 while (N->getNumOperands() &&
58 N->getOperand(N->getNumOperands()-1).getValueType() == MVT::Flag) {
59 N = N->getOperand(N->getNumOperands()-1).Val;
60 NodeSUnit->FlaggedNodes.push_back(N);
61 SUnitMap[N] = NodeSUnit;
62 }
63
64 // Scan down, adding this node and any flagged succs to FlaggedNodes if they
65 // have a user of the flag operand.
66 N = NI;
67 while (N->getValueType(N->getNumValues()-1) == MVT::Flag) {
68 SDOperand FlagVal(N, N->getNumValues()-1);
69
70 // There are either zero or one users of the Flag result.
71 bool HasFlagUse = false;
72 for (SDNode::use_iterator UI = N->use_begin(), E = N->use_end();
73 UI != E; ++UI)
74 if (FlagVal.isOperand(*UI)) {
75 HasFlagUse = true;
76 NodeSUnit->FlaggedNodes.push_back(N);
77 SUnitMap[N] = NodeSUnit;
78 N = *UI;
79 break;
80 }
81 if (!HasFlagUse) break;
82 }
83
84 // Now all flagged nodes are in FlaggedNodes and N is the bottom-most node.
85 // Update the SUnit
86 NodeSUnit->Node = N;
87 SUnitMap[N] = NodeSUnit;
88
89 // Compute the latency for the node. We use the sum of the latencies for
90 // all nodes flagged together into this SUnit.
91 if (InstrItins.isEmpty()) {
92 // No latency information.
93 NodeSUnit->Latency = 1;
94 } else {
95 NodeSUnit->Latency = 0;
96 if (N->isTargetOpcode()) {
97 unsigned SchedClass = TII->getSchedClass(N->getTargetOpcode());
98 InstrStage *S = InstrItins.begin(SchedClass);
99 InstrStage *E = InstrItins.end(SchedClass);
100 for (; S != E; ++S)
101 NodeSUnit->Latency += S->Cycles;
102 }
103 for (unsigned i = 0, e = NodeSUnit->FlaggedNodes.size(); i != e; ++i) {
104 SDNode *FNode = NodeSUnit->FlaggedNodes[i];
105 if (FNode->isTargetOpcode()) {
106 unsigned SchedClass = TII->getSchedClass(FNode->getTargetOpcode());
107 InstrStage *S = InstrItins.begin(SchedClass);
108 InstrStage *E = InstrItins.end(SchedClass);
109 for (; S != E; ++S)
110 NodeSUnit->Latency += S->Cycles;
111 }
112 }
113 }
114 }
115
116 // Pass 2: add the preds, succs, etc.
117 for (unsigned su = 0, e = SUnits.size(); su != e; ++su) {
118 SUnit *SU = &SUnits[su];
119 SDNode *MainNode = SU->Node;
120
121 if (MainNode->isTargetOpcode()) {
122 unsigned Opc = MainNode->getTargetOpcode();
Evan Cheng13d41b92006-05-12 01:58:24 +0000123 if (TII->isTwoAddrInstr(Opc))
Evan Chenge165a782006-05-11 23:55:42 +0000124 SU->isTwoAddress = true;
Evan Cheng13d41b92006-05-12 01:58:24 +0000125 if (TII->isCommutableInstr(Opc))
126 SU->isCommutable = true;
Evan Chenge165a782006-05-11 23:55:42 +0000127 }
128
129 // Find all predecessors and successors of the group.
130 // Temporarily add N to make code simpler.
131 SU->FlaggedNodes.push_back(MainNode);
132
133 for (unsigned n = 0, e = SU->FlaggedNodes.size(); n != e; ++n) {
134 SDNode *N = SU->FlaggedNodes[n];
135
136 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
137 SDNode *OpN = N->getOperand(i).Val;
138 if (isPassiveNode(OpN)) continue; // Not scheduled.
139 SUnit *OpSU = SUnitMap[OpN];
140 assert(OpSU && "Node has no SUnit!");
141 if (OpSU == SU) continue; // In the same group.
142
143 MVT::ValueType OpVT = N->getOperand(i).getValueType();
144 assert(OpVT != MVT::Flag && "Flagged nodes should be in same sunit!");
145 bool isChain = OpVT == MVT::Other;
146
147 if (SU->Preds.insert(std::make_pair(OpSU, isChain)).second) {
148 if (!isChain) {
149 SU->NumPreds++;
150 SU->NumPredsLeft++;
151 } else {
152 SU->NumChainPredsLeft++;
153 }
154 }
155 if (OpSU->Succs.insert(std::make_pair(SU, isChain)).second) {
156 if (!isChain) {
157 OpSU->NumSuccs++;
158 OpSU->NumSuccsLeft++;
159 } else {
160 OpSU->NumChainSuccsLeft++;
161 }
162 }
163 }
164 }
165
166 // Remove MainNode from FlaggedNodes again.
167 SU->FlaggedNodes.pop_back();
168 }
169
170 return;
171}
172
Evan Cheng626da3d2006-05-12 06:05:18 +0000173static void CalculateDepths(SUnit *SU, unsigned Depth, unsigned Max) {
174 if (Depth > SU->Depth) {
175 SU->Depth = Depth;
176 for (std::set<std::pair<SUnit*, bool> >::iterator I = SU->Succs.begin(),
177 E = SU->Succs.end(); I != E; ++I)
178 CalculateDepths(I->first, Depth+1, Max);
179 }
Evan Chenge165a782006-05-11 23:55:42 +0000180}
181
182void ScheduleDAG::CalculateDepths() {
183 SUnit *Entry = SUnitMap[DAG.getEntryNode().Val];
Evan Cheng626da3d2006-05-12 06:05:18 +0000184 ::CalculateDepths(Entry, 0U, SUnits.size());
Evan Chenge165a782006-05-11 23:55:42 +0000185 for (unsigned i = 0, e = SUnits.size(); i != e; ++i)
186 if (SUnits[i].Preds.size() == 0 && &SUnits[i] != Entry) {
Evan Cheng626da3d2006-05-12 06:05:18 +0000187 ::CalculateDepths(&SUnits[i], 0U, SUnits.size());
Evan Chenge165a782006-05-11 23:55:42 +0000188 }
189}
190
191static void CalculateHeights(SUnit *SU, unsigned Height) {
Evan Cheng626da3d2006-05-12 06:05:18 +0000192 if (Height > SU->Height) {
193 SU->Height = Height;
194 for (std::set<std::pair<SUnit*, bool> >::iterator I = SU->Preds.begin(),
195 E = SU->Preds.end(); I != E; ++I)
196 CalculateHeights(I->first, Height+1);
197 }
Evan Chenge165a782006-05-11 23:55:42 +0000198}
199void ScheduleDAG::CalculateHeights() {
200 SUnit *Root = SUnitMap[DAG.getRoot().Val];
201 ::CalculateHeights(Root, 0U);
202}
203
Jim Laskeye6b90fb2005-09-26 21:57:04 +0000204/// CountResults - The results of target nodes have register or immediate
205/// operands first, then an optional chain, and optional flag operands (which do
206/// not go into the machine instrs.)
Evan Chenga9c20912006-01-21 02:32:06 +0000207static unsigned CountResults(SDNode *Node) {
Jim Laskeyb6d4c2c2005-09-30 19:15:27 +0000208 unsigned N = Node->getNumValues();
209 while (N && Node->getValueType(N - 1) == MVT::Flag)
Jim Laskeye6b90fb2005-09-26 21:57:04 +0000210 --N;
Jim Laskeyb6d4c2c2005-09-30 19:15:27 +0000211 if (N && Node->getValueType(N - 1) == MVT::Other)
Jim Laskeye6b90fb2005-09-26 21:57:04 +0000212 --N; // Skip over chain result.
213 return N;
214}
215
216/// CountOperands The inputs to target nodes have any actual inputs first,
217/// followed by an optional chain operand, then flag operands. Compute the
218/// number of actual operands that will go into the machine instr.
Evan Chenga9c20912006-01-21 02:32:06 +0000219static unsigned CountOperands(SDNode *Node) {
Jim Laskeyb6d4c2c2005-09-30 19:15:27 +0000220 unsigned N = Node->getNumOperands();
221 while (N && Node->getOperand(N - 1).getValueType() == MVT::Flag)
Jim Laskeye6b90fb2005-09-26 21:57:04 +0000222 --N;
Jim Laskeyb6d4c2c2005-09-30 19:15:27 +0000223 if (N && Node->getOperand(N - 1).getValueType() == MVT::Other)
Jim Laskeye6b90fb2005-09-26 21:57:04 +0000224 --N; // Ignore chain if it exists.
225 return N;
226}
227
Evan Cheng4ef10862006-01-23 07:01:07 +0000228static unsigned CreateVirtualRegisters(MachineInstr *MI,
229 unsigned NumResults,
230 SSARegMap *RegMap,
231 const TargetInstrDescriptor &II) {
Jim Laskeye6b90fb2005-09-26 21:57:04 +0000232 // Create the result registers for this node and add the result regs to
233 // the machine instruction.
234 const TargetOperandInfo *OpInfo = II.OpInfo;
235 unsigned ResultReg = RegMap->createVirtualRegister(OpInfo[0].RegClass);
236 MI->addRegOperand(ResultReg, MachineOperand::Def);
237 for (unsigned i = 1; i != NumResults; ++i) {
238 assert(OpInfo[i].RegClass && "Isn't a register operand!");
Chris Lattner505277a2005-10-01 07:45:09 +0000239 MI->addRegOperand(RegMap->createVirtualRegister(OpInfo[i].RegClass),
Jim Laskeye6b90fb2005-09-26 21:57:04 +0000240 MachineOperand::Def);
241 }
242 return ResultReg;
243}
244
Chris Lattnerdf375062006-03-10 07:25:12 +0000245/// getVR - Return the virtual register corresponding to the specified result
246/// of the specified node.
247static unsigned getVR(SDOperand Op, std::map<SDNode*, unsigned> &VRBaseMap) {
248 std::map<SDNode*, unsigned>::iterator I = VRBaseMap.find(Op.Val);
249 assert(I != VRBaseMap.end() && "Node emitted out of order - late");
250 return I->second + Op.ResNo;
251}
252
253
Chris Lattnered18b682006-02-24 18:54:03 +0000254/// AddOperand - Add the specified operand to the specified machine instr. II
255/// specifies the instruction information for the node, and IIOpNum is the
256/// operand number (in the II) that we are adding. IIOpNum and II are used for
257/// assertions only.
258void ScheduleDAG::AddOperand(MachineInstr *MI, SDOperand Op,
259 unsigned IIOpNum,
Chris Lattnerdf375062006-03-10 07:25:12 +0000260 const TargetInstrDescriptor *II,
261 std::map<SDNode*, unsigned> &VRBaseMap) {
Chris Lattnered18b682006-02-24 18:54:03 +0000262 if (Op.isTargetOpcode()) {
263 // Note that this case is redundant with the final else block, but we
264 // include it because it is the most common and it makes the logic
265 // simpler here.
266 assert(Op.getValueType() != MVT::Other &&
267 Op.getValueType() != MVT::Flag &&
268 "Chain and flag operands should occur at end of operand list!");
269
270 // Get/emit the operand.
Chris Lattnerdf375062006-03-10 07:25:12 +0000271 unsigned VReg = getVR(Op, VRBaseMap);
Chris Lattnered18b682006-02-24 18:54:03 +0000272 MI->addRegOperand(VReg, MachineOperand::Use);
273
274 // Verify that it is right.
275 assert(MRegisterInfo::isVirtualRegister(VReg) && "Not a vreg?");
276 if (II) {
277 assert(II->OpInfo[IIOpNum].RegClass &&
278 "Don't have operand info for this instruction!");
279 assert(RegMap->getRegClass(VReg) == II->OpInfo[IIOpNum].RegClass &&
280 "Register class of operand and regclass of use don't agree!");
281 }
282 } else if (ConstantSDNode *C =
283 dyn_cast<ConstantSDNode>(Op)) {
Chris Lattner2d90ac72006-05-04 18:05:43 +0000284 MI->addImmOperand(C->getValue());
Chris Lattnered18b682006-02-24 18:54:03 +0000285 } else if (RegisterSDNode*R =
286 dyn_cast<RegisterSDNode>(Op)) {
287 MI->addRegOperand(R->getReg(), MachineOperand::Use);
288 } else if (GlobalAddressSDNode *TGA =
289 dyn_cast<GlobalAddressSDNode>(Op)) {
Chris Lattnerea50fab2006-05-04 01:15:02 +0000290 MI->addGlobalAddressOperand(TGA->getGlobal(), TGA->getOffset());
Chris Lattnered18b682006-02-24 18:54:03 +0000291 } else if (BasicBlockSDNode *BB =
292 dyn_cast<BasicBlockSDNode>(Op)) {
293 MI->addMachineBasicBlockOperand(BB->getBasicBlock());
294 } else if (FrameIndexSDNode *FI =
295 dyn_cast<FrameIndexSDNode>(Op)) {
296 MI->addFrameIndexOperand(FI->getIndex());
Nate Begeman37efe672006-04-22 18:53:45 +0000297 } else if (JumpTableSDNode *JT =
298 dyn_cast<JumpTableSDNode>(Op)) {
299 MI->addJumpTableIndexOperand(JT->getIndex());
Chris Lattnered18b682006-02-24 18:54:03 +0000300 } else if (ConstantPoolSDNode *CP =
301 dyn_cast<ConstantPoolSDNode>(Op)) {
Evan Cheng404cb4f2006-02-25 09:54:52 +0000302 int Offset = CP->getOffset();
Chris Lattnered18b682006-02-24 18:54:03 +0000303 unsigned Align = CP->getAlignment();
304 // MachineConstantPool wants an explicit alignment.
305 if (Align == 0) {
306 if (CP->get()->getType() == Type::DoubleTy)
307 Align = 3; // always 8-byte align doubles.
Chris Lattner54a30b92006-03-20 01:51:46 +0000308 else {
Chris Lattnered18b682006-02-24 18:54:03 +0000309 Align = TM.getTargetData()
Owen Andersona69571c2006-05-03 01:29:57 +0000310 ->getTypeAlignmentShift(CP->get()->getType());
Chris Lattner54a30b92006-03-20 01:51:46 +0000311 if (Align == 0) {
312 // Alignment of packed types. FIXME!
Owen Andersona69571c2006-05-03 01:29:57 +0000313 Align = TM.getTargetData()->getTypeSize(CP->get()->getType());
Chris Lattner54a30b92006-03-20 01:51:46 +0000314 Align = Log2_64(Align);
315 }
316 }
Chris Lattnered18b682006-02-24 18:54:03 +0000317 }
318
319 unsigned Idx = ConstPool->getConstantPoolIndex(CP->get(), Align);
Evan Cheng404cb4f2006-02-25 09:54:52 +0000320 MI->addConstantPoolIndexOperand(Idx, Offset);
Chris Lattnered18b682006-02-24 18:54:03 +0000321 } else if (ExternalSymbolSDNode *ES =
322 dyn_cast<ExternalSymbolSDNode>(Op)) {
Chris Lattnerea50fab2006-05-04 01:15:02 +0000323 MI->addExternalSymbolOperand(ES->getSymbol());
Chris Lattnered18b682006-02-24 18:54:03 +0000324 } else {
325 assert(Op.getValueType() != MVT::Other &&
326 Op.getValueType() != MVT::Flag &&
327 "Chain and flag operands should occur at end of operand list!");
Chris Lattnerdf375062006-03-10 07:25:12 +0000328 unsigned VReg = getVR(Op, VRBaseMap);
Chris Lattnered18b682006-02-24 18:54:03 +0000329 MI->addRegOperand(VReg, MachineOperand::Use);
330
331 // Verify that it is right.
332 assert(MRegisterInfo::isVirtualRegister(VReg) && "Not a vreg?");
333 if (II) {
334 assert(II->OpInfo[IIOpNum].RegClass &&
335 "Don't have operand info for this instruction!");
336 assert(RegMap->getRegClass(VReg) == II->OpInfo[IIOpNum].RegClass &&
337 "Register class of operand and regclass of use don't agree!");
338 }
339 }
340
341}
342
343
Jim Laskeyb6d4c2c2005-09-30 19:15:27 +0000344/// EmitNode - Generate machine code for an node and needed dependencies.
Jim Laskeye6b90fb2005-09-26 21:57:04 +0000345///
Chris Lattner8c7ef052006-03-10 07:28:36 +0000346void ScheduleDAG::EmitNode(SDNode *Node,
Chris Lattnerdf375062006-03-10 07:25:12 +0000347 std::map<SDNode*, unsigned> &VRBaseMap) {
Jim Laskeyb6d4c2c2005-09-30 19:15:27 +0000348 unsigned VRBase = 0; // First virtual register for node
Chris Lattner2d973e42005-08-18 20:07:59 +0000349
Jim Laskeyb6d4c2c2005-09-30 19:15:27 +0000350 // If machine instruction
351 if (Node->isTargetOpcode()) {
352 unsigned Opc = Node->getTargetOpcode();
Evan Chenga9c20912006-01-21 02:32:06 +0000353 const TargetInstrDescriptor &II = TII->get(Opc);
Chris Lattner2d973e42005-08-18 20:07:59 +0000354
Jim Laskeyb6d4c2c2005-09-30 19:15:27 +0000355 unsigned NumResults = CountResults(Node);
356 unsigned NodeOperands = CountOperands(Node);
Jim Laskeye6b90fb2005-09-26 21:57:04 +0000357 unsigned NumMIOperands = NodeOperands + NumResults;
Chris Lattnerda8abb02005-09-01 18:44:10 +0000358#ifndef NDEBUG
Chris Lattner14b392a2005-08-24 22:02:41 +0000359 assert((unsigned(II.numOperands) == NumMIOperands || II.numOperands == -1)&&
Chris Lattner2d973e42005-08-18 20:07:59 +0000360 "#operands for dag node doesn't match .td file!");
Chris Lattnerca6aa2f2005-08-19 01:01:34 +0000361#endif
Chris Lattner2d973e42005-08-18 20:07:59 +0000362
363 // Create the new machine instruction.
Chris Lattner8b915b42006-05-04 18:16:01 +0000364 MachineInstr *MI = new MachineInstr(Opc, NumMIOperands);
Chris Lattner2d973e42005-08-18 20:07:59 +0000365
366 // Add result register values for things that are defined by this
367 // instruction.
Chris Lattnera4176522005-10-30 18:54:27 +0000368
369 // If the node is only used by a CopyToReg and the dest reg is a vreg, use
370 // the CopyToReg'd destination register instead of creating a new vreg.
371 if (NumResults == 1) {
372 for (SDNode::use_iterator UI = Node->use_begin(), E = Node->use_end();
373 UI != E; ++UI) {
374 SDNode *Use = *UI;
375 if (Use->getOpcode() == ISD::CopyToReg &&
376 Use->getOperand(2).Val == Node) {
377 unsigned Reg = cast<RegisterSDNode>(Use->getOperand(1))->getReg();
378 if (MRegisterInfo::isVirtualRegister(Reg)) {
379 VRBase = Reg;
380 MI->addRegOperand(Reg, MachineOperand::Def);
381 break;
382 }
383 }
384 }
385 }
386
387 // Otherwise, create new virtual registers.
388 if (NumResults && VRBase == 0)
Evan Cheng4ef10862006-01-23 07:01:07 +0000389 VRBase = CreateVirtualRegisters(MI, NumResults, RegMap, II);
Jim Laskeyb6d4c2c2005-09-30 19:15:27 +0000390
391 // Emit all of the actual operands of this instruction, adding them to the
392 // instruction as appropriate.
Chris Lattnered18b682006-02-24 18:54:03 +0000393 for (unsigned i = 0; i != NodeOperands; ++i)
Chris Lattnerdf375062006-03-10 07:25:12 +0000394 AddOperand(MI, Node->getOperand(i), i+NumResults, &II, VRBaseMap);
Evan Cheng13d41b92006-05-12 01:58:24 +0000395
396 // Commute node if it has been determined to be profitable.
397 if (CommuteSet.count(Node)) {
398 MachineInstr *NewMI = TII->commuteInstruction(MI);
399 if (NewMI == 0)
400 DEBUG(std::cerr << "Sched: COMMUTING FAILED!\n");
401 else {
402 DEBUG(std::cerr << "Sched: COMMUTED TO: " << *NewMI);
403 MI = NewMI;
404 }
405 }
406
Jim Laskeyb6d4c2c2005-09-30 19:15:27 +0000407 // Now that we have emitted all operands, emit this instruction itself.
408 if ((II.Flags & M_USES_CUSTOM_DAG_SCHED_INSERTION) == 0) {
409 BB->insert(BB->end(), MI);
410 } else {
411 // Insert this instruction into the end of the basic block, potentially
412 // taking some custom action.
413 BB = DAG.getTargetLoweringInfo().InsertAtEndOfBasicBlock(MI, BB);
414 }
415 } else {
416 switch (Node->getOpcode()) {
417 default:
418 Node->dump();
419 assert(0 && "This target-independent node should have been selected!");
420 case ISD::EntryToken: // fall thru
421 case ISD::TokenFactor:
422 break;
423 case ISD::CopyToReg: {
Chris Lattnerdf375062006-03-10 07:25:12 +0000424 unsigned InReg = getVR(Node->getOperand(2), VRBaseMap);
Chris Lattnera4176522005-10-30 18:54:27 +0000425 unsigned DestReg = cast<RegisterSDNode>(Node->getOperand(1))->getReg();
Chris Lattner45053fc2006-03-24 07:15:07 +0000426 if (InReg != DestReg) // Coalesced away the copy?
Evan Chenga9c20912006-01-21 02:32:06 +0000427 MRI->copyRegToReg(*BB, BB->end(), DestReg, InReg,
428 RegMap->getRegClass(InReg));
Jim Laskeyb6d4c2c2005-09-30 19:15:27 +0000429 break;
430 }
431 case ISD::CopyFromReg: {
432 unsigned SrcReg = cast<RegisterSDNode>(Node->getOperand(1))->getReg();
Chris Lattner089c25c2005-10-09 05:58:56 +0000433 if (MRegisterInfo::isVirtualRegister(SrcReg)) {
434 VRBase = SrcReg; // Just use the input register directly!
435 break;
436 }
437
Chris Lattnera4176522005-10-30 18:54:27 +0000438 // If the node is only used by a CopyToReg and the dest reg is a vreg, use
439 // the CopyToReg'd destination register instead of creating a new vreg.
440 for (SDNode::use_iterator UI = Node->use_begin(), E = Node->use_end();
441 UI != E; ++UI) {
442 SDNode *Use = *UI;
443 if (Use->getOpcode() == ISD::CopyToReg &&
444 Use->getOperand(2).Val == Node) {
445 unsigned DestReg = cast<RegisterSDNode>(Use->getOperand(1))->getReg();
446 if (MRegisterInfo::isVirtualRegister(DestReg)) {
447 VRBase = DestReg;
448 break;
449 }
450 }
451 }
452
Jim Laskeyb6d4c2c2005-09-30 19:15:27 +0000453 // Figure out the register class to create for the destreg.
454 const TargetRegisterClass *TRC = 0;
Chris Lattnera4176522005-10-30 18:54:27 +0000455 if (VRBase) {
456 TRC = RegMap->getRegClass(VRBase);
457 } else {
Chris Lattner089c25c2005-10-09 05:58:56 +0000458
Chris Lattnera4176522005-10-30 18:54:27 +0000459 // Pick the register class of the right type that contains this physreg.
Evan Chenga9c20912006-01-21 02:32:06 +0000460 for (MRegisterInfo::regclass_iterator I = MRI->regclass_begin(),
461 E = MRI->regclass_end(); I != E; ++I)
Nate Begeman6510b222005-12-01 04:51:06 +0000462 if ((*I)->hasType(Node->getValueType(0)) &&
Chris Lattnera4176522005-10-30 18:54:27 +0000463 (*I)->contains(SrcReg)) {
464 TRC = *I;
465 break;
466 }
467 assert(TRC && "Couldn't find register class for reg copy!");
Jim Laskeyb6d4c2c2005-09-30 19:15:27 +0000468
Chris Lattnera4176522005-10-30 18:54:27 +0000469 // Create the reg, emit the copy.
470 VRBase = RegMap->createVirtualRegister(TRC);
471 }
Evan Chenga9c20912006-01-21 02:32:06 +0000472 MRI->copyRegToReg(*BB, BB->end(), VRBase, SrcReg, TRC);
Jim Laskeyb6d4c2c2005-09-30 19:15:27 +0000473 break;
474 }
Chris Lattneracc43bf2006-01-26 23:28:04 +0000475 case ISD::INLINEASM: {
476 unsigned NumOps = Node->getNumOperands();
477 if (Node->getOperand(NumOps-1).getValueType() == MVT::Flag)
478 --NumOps; // Ignore the flag operand.
479
480 // Create the inline asm machine instruction.
481 MachineInstr *MI =
482 new MachineInstr(BB, TargetInstrInfo::INLINEASM, (NumOps-2)/2+1);
483
484 // Add the asm string as an external symbol operand.
485 const char *AsmStr =
486 cast<ExternalSymbolSDNode>(Node->getOperand(1))->getSymbol();
Chris Lattnerea50fab2006-05-04 01:15:02 +0000487 MI->addExternalSymbolOperand(AsmStr);
Chris Lattneracc43bf2006-01-26 23:28:04 +0000488
489 // Add all of the operand registers to the instruction.
Chris Lattnerc3a9f8d2006-02-23 19:21:04 +0000490 for (unsigned i = 2; i != NumOps;) {
491 unsigned Flags = cast<ConstantSDNode>(Node->getOperand(i))->getValue();
Chris Lattnerfd6d2822006-02-24 19:18:20 +0000492 unsigned NumVals = Flags >> 3;
Chris Lattnerc3a9f8d2006-02-23 19:21:04 +0000493
Chris Lattner2d90ac72006-05-04 18:05:43 +0000494 MI->addImmOperand(Flags);
Chris Lattnerc3a9f8d2006-02-23 19:21:04 +0000495 ++i; // Skip the ID value.
496
497 switch (Flags & 7) {
Chris Lattneracc43bf2006-01-26 23:28:04 +0000498 default: assert(0 && "Bad flags!");
Chris Lattnerc3a9f8d2006-02-23 19:21:04 +0000499 case 1: // Use of register.
Chris Lattnerfd6d2822006-02-24 19:18:20 +0000500 for (; NumVals; --NumVals, ++i) {
Chris Lattnerc3a9f8d2006-02-23 19:21:04 +0000501 unsigned Reg = cast<RegisterSDNode>(Node->getOperand(i))->getReg();
Chris Lattnerea50fab2006-05-04 01:15:02 +0000502 MI->addRegOperand(Reg, MachineOperand::Use);
Chris Lattnerc3a9f8d2006-02-23 19:21:04 +0000503 }
Chris Lattnerdc19b702006-02-04 02:26:14 +0000504 break;
Chris Lattnerc3a9f8d2006-02-23 19:21:04 +0000505 case 2: // Def of register.
Chris Lattnerfd6d2822006-02-24 19:18:20 +0000506 for (; NumVals; --NumVals, ++i) {
Chris Lattnerc3a9f8d2006-02-23 19:21:04 +0000507 unsigned Reg = cast<RegisterSDNode>(Node->getOperand(i))->getReg();
Chris Lattnerea50fab2006-05-04 01:15:02 +0000508 MI->addRegOperand(Reg, MachineOperand::Def);
Chris Lattnerc3a9f8d2006-02-23 19:21:04 +0000509 }
Chris Lattnerdc19b702006-02-04 02:26:14 +0000510 break;
Chris Lattnerdc19b702006-02-04 02:26:14 +0000511 case 3: { // Immediate.
Chris Lattnerfd6d2822006-02-24 19:18:20 +0000512 assert(NumVals == 1 && "Unknown immediate value!");
Chris Lattnerdc19b702006-02-04 02:26:14 +0000513 uint64_t Val = cast<ConstantSDNode>(Node->getOperand(i))->getValue();
Chris Lattner2d90ac72006-05-04 18:05:43 +0000514 MI->addImmOperand(Val);
Chris Lattnerc3a9f8d2006-02-23 19:21:04 +0000515 ++i;
Chris Lattnerdc19b702006-02-04 02:26:14 +0000516 break;
517 }
Chris Lattnerfd6d2822006-02-24 19:18:20 +0000518 case 4: // Addressing mode.
519 // The addressing mode has been selected, just add all of the
520 // operands to the machine instruction.
521 for (; NumVals; --NumVals, ++i)
Chris Lattnerdf375062006-03-10 07:25:12 +0000522 AddOperand(MI, Node->getOperand(i), 0, 0, VRBaseMap);
Chris Lattnerfd6d2822006-02-24 19:18:20 +0000523 break;
Chris Lattnerdc19b702006-02-04 02:26:14 +0000524 }
Chris Lattneracc43bf2006-01-26 23:28:04 +0000525 }
526 break;
527 }
Jim Laskeyb6d4c2c2005-09-30 19:15:27 +0000528 }
529 }
530
Chris Lattnerdf375062006-03-10 07:25:12 +0000531 assert(!VRBaseMap.count(Node) && "Node emitted out of order - early");
532 VRBaseMap[Node] = VRBase;
Jim Laskeyb6d4c2c2005-09-30 19:15:27 +0000533}
534
Chris Lattnera93dfcd2006-03-05 23:51:47 +0000535void ScheduleDAG::EmitNoop() {
536 TII->insertNoop(*BB, BB->end());
537}
538
Evan Chenge165a782006-05-11 23:55:42 +0000539/// EmitSchedule - Emit the machine code in scheduled order.
540void ScheduleDAG::EmitSchedule() {
541 std::map<SDNode*, unsigned> VRBaseMap;
542 for (unsigned i = 0, e = Sequence.size(); i != e; i++) {
543 if (SUnit *SU = Sequence[i]) {
544 for (unsigned j = 0, ee = SU->FlaggedNodes.size(); j != ee; j++)
545 EmitNode(SU->FlaggedNodes[j], VRBaseMap);
546 EmitNode(SU->Node, VRBaseMap);
547 } else {
548 // Null SUnit* is a noop.
549 EmitNoop();
550 }
551 }
552}
553
554/// dump - dump the schedule.
555void ScheduleDAG::dumpSchedule() const {
556 for (unsigned i = 0, e = Sequence.size(); i != e; i++) {
557 if (SUnit *SU = Sequence[i])
558 SU->dump(&DAG);
559 else
560 std::cerr << "**** NOOP ****\n";
561 }
562}
563
564
Evan Chenga9c20912006-01-21 02:32:06 +0000565/// Run - perform scheduling.
566///
567MachineBasicBlock *ScheduleDAG::Run() {
568 TII = TM.getInstrInfo();
569 MRI = TM.getRegisterInfo();
570 RegMap = BB->getParent()->getSSARegMap();
571 ConstPool = BB->getParent()->getConstantPool();
Evan Cheng4ef10862006-01-23 07:01:07 +0000572
Evan Chenga9c20912006-01-21 02:32:06 +0000573 Schedule();
574 return BB;
Chris Lattnerd32b2362005-08-18 18:45:24 +0000575}
Evan Cheng4ef10862006-01-23 07:01:07 +0000576
Evan Chenge165a782006-05-11 23:55:42 +0000577/// SUnit - Scheduling unit. It's an wrapper around either a single SDNode or
578/// a group of nodes flagged together.
579void SUnit::dump(const SelectionDAG *G) const {
580 std::cerr << "SU(" << NodeNum << "): ";
581 Node->dump(G);
582 std::cerr << "\n";
583 if (FlaggedNodes.size() != 0) {
584 for (unsigned i = 0, e = FlaggedNodes.size(); i != e; i++) {
585 std::cerr << " ";
586 FlaggedNodes[i]->dump(G);
587 std::cerr << "\n";
588 }
589 }
590}
Evan Cheng4ef10862006-01-23 07:01:07 +0000591
Evan Chenge165a782006-05-11 23:55:42 +0000592void SUnit::dumpAll(const SelectionDAG *G) const {
593 dump(G);
594
595 std::cerr << " # preds left : " << NumPredsLeft << "\n";
596 std::cerr << " # succs left : " << NumSuccsLeft << "\n";
597 std::cerr << " # chain preds left : " << NumChainPredsLeft << "\n";
598 std::cerr << " # chain succs left : " << NumChainSuccsLeft << "\n";
599 std::cerr << " Latency : " << Latency << "\n";
600 std::cerr << " Depth : " << Depth << "\n";
601 std::cerr << " Height : " << Height << "\n";
602
603 if (Preds.size() != 0) {
604 std::cerr << " Predecessors:\n";
605 for (std::set<std::pair<SUnit*,bool> >::const_iterator I = Preds.begin(),
606 E = Preds.end(); I != E; ++I) {
607 if (I->second)
608 std::cerr << " ch ";
609 else
610 std::cerr << " val ";
611 I->first->dump(G);
612 }
613 }
614 if (Succs.size() != 0) {
615 std::cerr << " Successors:\n";
616 for (std::set<std::pair<SUnit*, bool> >::const_iterator I = Succs.begin(),
617 E = Succs.end(); I != E; ++I) {
618 if (I->second)
619 std::cerr << " ch ";
620 else
621 std::cerr << " val ";
622 I->first->dump(G);
623 }
624 }
625 std::cerr << "\n";
626}