blob: 9bc168f7a6d7dea4b2009d097088f4e45f226c4b [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
16#define DEBUG_TYPE "sched"
Chris Lattner5839bf22005-08-26 17:15:30 +000017#include "llvm/CodeGen/MachineConstantPool.h"
Chris Lattner4ccd4062005-08-19 20:45:43 +000018#include "llvm/CodeGen/MachineFunction.h"
Evan Chenga9c20912006-01-21 02:32:06 +000019#include "llvm/CodeGen/ScheduleDAG.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"
Jim Laskey7d090f32005-11-04 04:05:35 +000023#include "llvm/Target/TargetInstrItineraries.h"
Chris Lattner025c39b2005-08-26 20:54:47 +000024#include "llvm/Target/TargetLowering.h"
Jim Laskeye6b90fb2005-09-26 21:57:04 +000025#include "llvm/Support/Debug.h"
Chris Lattner948d9662006-02-09 02:23:13 +000026#include "llvm/Constant.h"
Jim Laskeye6b90fb2005-09-26 21:57:04 +000027#include <iostream>
Chris Lattnerd32b2362005-08-18 18:45:24 +000028using namespace llvm;
29
Jim Laskeye6b90fb2005-09-26 21:57:04 +000030
31/// CountResults - The results of target nodes have register or immediate
32/// operands first, then an optional chain, and optional flag operands (which do
33/// not go into the machine instrs.)
Evan Chenga9c20912006-01-21 02:32:06 +000034static unsigned CountResults(SDNode *Node) {
Jim Laskeyb6d4c2c2005-09-30 19:15:27 +000035 unsigned N = Node->getNumValues();
36 while (N && Node->getValueType(N - 1) == MVT::Flag)
Jim Laskeye6b90fb2005-09-26 21:57:04 +000037 --N;
Jim Laskeyb6d4c2c2005-09-30 19:15:27 +000038 if (N && Node->getValueType(N - 1) == MVT::Other)
Jim Laskeye6b90fb2005-09-26 21:57:04 +000039 --N; // Skip over chain result.
40 return N;
41}
42
43/// CountOperands The inputs to target nodes have any actual inputs first,
44/// followed by an optional chain operand, then flag operands. Compute the
45/// number of actual operands that will go into the machine instr.
Evan Chenga9c20912006-01-21 02:32:06 +000046static unsigned CountOperands(SDNode *Node) {
Jim Laskeyb6d4c2c2005-09-30 19:15:27 +000047 unsigned N = Node->getNumOperands();
48 while (N && Node->getOperand(N - 1).getValueType() == MVT::Flag)
Jim Laskeye6b90fb2005-09-26 21:57:04 +000049 --N;
Jim Laskeyb6d4c2c2005-09-30 19:15:27 +000050 if (N && Node->getOperand(N - 1).getValueType() == MVT::Other)
Jim Laskeye6b90fb2005-09-26 21:57:04 +000051 --N; // Ignore chain if it exists.
52 return N;
53}
54
Evan Cheng4ef10862006-01-23 07:01:07 +000055/// PrepareNodeInfo - Set up the basic minimum node info for scheduling.
56///
57void ScheduleDAG::PrepareNodeInfo() {
58 // Allocate node information
59 Info = new NodeInfo[NodeCount];
60
61 unsigned i = 0;
62 for (SelectionDAG::allnodes_iterator I = DAG.allnodes_begin(),
63 E = DAG.allnodes_end(); I != E; ++I, ++i) {
64 // Fast reference to node schedule info
65 NodeInfo* NI = &Info[i];
66 // Set up map
67 Map[I] = NI;
68 // Set node
69 NI->Node = I;
70 // Set pending visit count
71 NI->setPending(I->use_size());
72 }
73}
74
75/// IdentifyGroups - Put flagged nodes into groups.
76///
77void ScheduleDAG::IdentifyGroups() {
78 for (unsigned i = 0, N = NodeCount; i < N; i++) {
79 NodeInfo* NI = &Info[i];
80 SDNode *Node = NI->Node;
81
82 // For each operand (in reverse to only look at flags)
83 for (unsigned N = Node->getNumOperands(); 0 < N--;) {
84 // Get operand
85 SDOperand Op = Node->getOperand(N);
86 // No more flags to walk
87 if (Op.getValueType() != MVT::Flag) break;
88 // Add to node group
Evan Chengcccf1232006-02-04 06:49:00 +000089 AddToGroup(getNI(Op.Val), NI);
Evan Chenge0a58322006-01-25 09:13:41 +000090 // Let everyone else know
Evan Cheng4ef10862006-01-23 07:01:07 +000091 HasGroups = true;
92 }
93 }
94}
95
96static unsigned CreateVirtualRegisters(MachineInstr *MI,
97 unsigned NumResults,
98 SSARegMap *RegMap,
99 const TargetInstrDescriptor &II) {
Jim Laskeye6b90fb2005-09-26 21:57:04 +0000100 // Create the result registers for this node and add the result regs to
101 // the machine instruction.
102 const TargetOperandInfo *OpInfo = II.OpInfo;
103 unsigned ResultReg = RegMap->createVirtualRegister(OpInfo[0].RegClass);
104 MI->addRegOperand(ResultReg, MachineOperand::Def);
105 for (unsigned i = 1; i != NumResults; ++i) {
106 assert(OpInfo[i].RegClass && "Isn't a register operand!");
Chris Lattner505277a2005-10-01 07:45:09 +0000107 MI->addRegOperand(RegMap->createVirtualRegister(OpInfo[i].RegClass),
Jim Laskeye6b90fb2005-09-26 21:57:04 +0000108 MachineOperand::Def);
109 }
110 return ResultReg;
111}
112
Chris Lattnered18b682006-02-24 18:54:03 +0000113/// AddOperand - Add the specified operand to the specified machine instr. II
114/// specifies the instruction information for the node, and IIOpNum is the
115/// operand number (in the II) that we are adding. IIOpNum and II are used for
116/// assertions only.
117void ScheduleDAG::AddOperand(MachineInstr *MI, SDOperand Op,
118 unsigned IIOpNum,
119 const TargetInstrDescriptor *II) {
120 if (Op.isTargetOpcode()) {
121 // Note that this case is redundant with the final else block, but we
122 // include it because it is the most common and it makes the logic
123 // simpler here.
124 assert(Op.getValueType() != MVT::Other &&
125 Op.getValueType() != MVT::Flag &&
126 "Chain and flag operands should occur at end of operand list!");
127
128 // Get/emit the operand.
129 unsigned VReg = getVR(Op);
130 MI->addRegOperand(VReg, MachineOperand::Use);
131
132 // Verify that it is right.
133 assert(MRegisterInfo::isVirtualRegister(VReg) && "Not a vreg?");
134 if (II) {
135 assert(II->OpInfo[IIOpNum].RegClass &&
136 "Don't have operand info for this instruction!");
137 assert(RegMap->getRegClass(VReg) == II->OpInfo[IIOpNum].RegClass &&
138 "Register class of operand and regclass of use don't agree!");
139 }
140 } else if (ConstantSDNode *C =
141 dyn_cast<ConstantSDNode>(Op)) {
142 MI->addZeroExtImm64Operand(C->getValue());
143 } else if (RegisterSDNode*R =
144 dyn_cast<RegisterSDNode>(Op)) {
145 MI->addRegOperand(R->getReg(), MachineOperand::Use);
146 } else if (GlobalAddressSDNode *TGA =
147 dyn_cast<GlobalAddressSDNode>(Op)) {
148 MI->addGlobalAddressOperand(TGA->getGlobal(), false, TGA->getOffset());
149 } else if (BasicBlockSDNode *BB =
150 dyn_cast<BasicBlockSDNode>(Op)) {
151 MI->addMachineBasicBlockOperand(BB->getBasicBlock());
152 } else if (FrameIndexSDNode *FI =
153 dyn_cast<FrameIndexSDNode>(Op)) {
154 MI->addFrameIndexOperand(FI->getIndex());
155 } else if (ConstantPoolSDNode *CP =
156 dyn_cast<ConstantPoolSDNode>(Op)) {
157 unsigned Align = CP->getAlignment();
158 // MachineConstantPool wants an explicit alignment.
159 if (Align == 0) {
160 if (CP->get()->getType() == Type::DoubleTy)
161 Align = 3; // always 8-byte align doubles.
162 else
163 Align = TM.getTargetData()
164 .getTypeAlignmentShift(CP->get()->getType());
165 }
166
167 unsigned Idx = ConstPool->getConstantPoolIndex(CP->get(), Align);
168 MI->addConstantPoolIndexOperand(Idx);
169 } else if (ExternalSymbolSDNode *ES =
170 dyn_cast<ExternalSymbolSDNode>(Op)) {
171 MI->addExternalSymbolOperand(ES->getSymbol(), false);
172 } else {
173 assert(Op.getValueType() != MVT::Other &&
174 Op.getValueType() != MVT::Flag &&
175 "Chain and flag operands should occur at end of operand list!");
176 unsigned VReg = getVR(Op);
177 MI->addRegOperand(VReg, MachineOperand::Use);
178
179 // Verify that it is right.
180 assert(MRegisterInfo::isVirtualRegister(VReg) && "Not a vreg?");
181 if (II) {
182 assert(II->OpInfo[IIOpNum].RegClass &&
183 "Don't have operand info for this instruction!");
184 assert(RegMap->getRegClass(VReg) == II->OpInfo[IIOpNum].RegClass &&
185 "Register class of operand and regclass of use don't agree!");
186 }
187 }
188
189}
190
191
Jim Laskeyb6d4c2c2005-09-30 19:15:27 +0000192/// EmitNode - Generate machine code for an node and needed dependencies.
Jim Laskeye6b90fb2005-09-26 21:57:04 +0000193///
Evan Chenga9c20912006-01-21 02:32:06 +0000194void ScheduleDAG::EmitNode(NodeInfo *NI) {
Jim Laskeyb6d4c2c2005-09-30 19:15:27 +0000195 unsigned VRBase = 0; // First virtual register for node
196 SDNode *Node = NI->Node;
Chris Lattner2d973e42005-08-18 20:07:59 +0000197
Jim Laskeyb6d4c2c2005-09-30 19:15:27 +0000198 // If machine instruction
199 if (Node->isTargetOpcode()) {
200 unsigned Opc = Node->getTargetOpcode();
Evan Chenga9c20912006-01-21 02:32:06 +0000201 const TargetInstrDescriptor &II = TII->get(Opc);
Chris Lattner2d973e42005-08-18 20:07:59 +0000202
Jim Laskeyb6d4c2c2005-09-30 19:15:27 +0000203 unsigned NumResults = CountResults(Node);
204 unsigned NodeOperands = CountOperands(Node);
Jim Laskeye6b90fb2005-09-26 21:57:04 +0000205 unsigned NumMIOperands = NodeOperands + NumResults;
Chris Lattnerda8abb02005-09-01 18:44:10 +0000206#ifndef NDEBUG
Chris Lattner14b392a2005-08-24 22:02:41 +0000207 assert((unsigned(II.numOperands) == NumMIOperands || II.numOperands == -1)&&
Chris Lattner2d973e42005-08-18 20:07:59 +0000208 "#operands for dag node doesn't match .td file!");
Chris Lattnerca6aa2f2005-08-19 01:01:34 +0000209#endif
Chris Lattner2d973e42005-08-18 20:07:59 +0000210
211 // Create the new machine instruction.
Chris Lattner14b392a2005-08-24 22:02:41 +0000212 MachineInstr *MI = new MachineInstr(Opc, NumMIOperands, true, true);
Chris Lattner2d973e42005-08-18 20:07:59 +0000213
214 // Add result register values for things that are defined by this
215 // instruction.
Chris Lattnera4176522005-10-30 18:54:27 +0000216
217 // If the node is only used by a CopyToReg and the dest reg is a vreg, use
218 // the CopyToReg'd destination register instead of creating a new vreg.
219 if (NumResults == 1) {
220 for (SDNode::use_iterator UI = Node->use_begin(), E = Node->use_end();
221 UI != E; ++UI) {
222 SDNode *Use = *UI;
223 if (Use->getOpcode() == ISD::CopyToReg &&
224 Use->getOperand(2).Val == Node) {
225 unsigned Reg = cast<RegisterSDNode>(Use->getOperand(1))->getReg();
226 if (MRegisterInfo::isVirtualRegister(Reg)) {
227 VRBase = Reg;
228 MI->addRegOperand(Reg, MachineOperand::Def);
229 break;
230 }
231 }
232 }
233 }
234
235 // Otherwise, create new virtual registers.
236 if (NumResults && VRBase == 0)
Evan Cheng4ef10862006-01-23 07:01:07 +0000237 VRBase = CreateVirtualRegisters(MI, NumResults, RegMap, II);
Jim Laskeyb6d4c2c2005-09-30 19:15:27 +0000238
239 // Emit all of the actual operands of this instruction, adding them to the
240 // instruction as appropriate.
Chris Lattnered18b682006-02-24 18:54:03 +0000241 for (unsigned i = 0; i != NodeOperands; ++i)
242 AddOperand(MI, Node->getOperand(i), i+NumResults, &II);
Jim Laskeyb6d4c2c2005-09-30 19:15:27 +0000243
244 // Now that we have emitted all operands, emit this instruction itself.
245 if ((II.Flags & M_USES_CUSTOM_DAG_SCHED_INSERTION) == 0) {
246 BB->insert(BB->end(), MI);
247 } else {
248 // Insert this instruction into the end of the basic block, potentially
249 // taking some custom action.
250 BB = DAG.getTargetLoweringInfo().InsertAtEndOfBasicBlock(MI, BB);
251 }
252 } else {
253 switch (Node->getOpcode()) {
254 default:
255 Node->dump();
256 assert(0 && "This target-independent node should have been selected!");
257 case ISD::EntryToken: // fall thru
258 case ISD::TokenFactor:
259 break;
260 case ISD::CopyToReg: {
Chris Lattnera4176522005-10-30 18:54:27 +0000261 unsigned InReg = getVR(Node->getOperand(2));
262 unsigned DestReg = cast<RegisterSDNode>(Node->getOperand(1))->getReg();
263 if (InReg != DestReg) // Coallesced away the copy?
Evan Chenga9c20912006-01-21 02:32:06 +0000264 MRI->copyRegToReg(*BB, BB->end(), DestReg, InReg,
265 RegMap->getRegClass(InReg));
Jim Laskeyb6d4c2c2005-09-30 19:15:27 +0000266 break;
267 }
268 case ISD::CopyFromReg: {
269 unsigned SrcReg = cast<RegisterSDNode>(Node->getOperand(1))->getReg();
Chris Lattner089c25c2005-10-09 05:58:56 +0000270 if (MRegisterInfo::isVirtualRegister(SrcReg)) {
271 VRBase = SrcReg; // Just use the input register directly!
272 break;
273 }
274
Chris Lattnera4176522005-10-30 18:54:27 +0000275 // If the node is only used by a CopyToReg and the dest reg is a vreg, use
276 // the CopyToReg'd destination register instead of creating a new vreg.
277 for (SDNode::use_iterator UI = Node->use_begin(), E = Node->use_end();
278 UI != E; ++UI) {
279 SDNode *Use = *UI;
280 if (Use->getOpcode() == ISD::CopyToReg &&
281 Use->getOperand(2).Val == Node) {
282 unsigned DestReg = cast<RegisterSDNode>(Use->getOperand(1))->getReg();
283 if (MRegisterInfo::isVirtualRegister(DestReg)) {
284 VRBase = DestReg;
285 break;
286 }
287 }
288 }
289
Jim Laskeyb6d4c2c2005-09-30 19:15:27 +0000290 // Figure out the register class to create for the destreg.
291 const TargetRegisterClass *TRC = 0;
Chris Lattnera4176522005-10-30 18:54:27 +0000292 if (VRBase) {
293 TRC = RegMap->getRegClass(VRBase);
294 } else {
Chris Lattner089c25c2005-10-09 05:58:56 +0000295
Chris Lattnera4176522005-10-30 18:54:27 +0000296 // Pick the register class of the right type that contains this physreg.
Evan Chenga9c20912006-01-21 02:32:06 +0000297 for (MRegisterInfo::regclass_iterator I = MRI->regclass_begin(),
298 E = MRI->regclass_end(); I != E; ++I)
Nate Begeman6510b222005-12-01 04:51:06 +0000299 if ((*I)->hasType(Node->getValueType(0)) &&
Chris Lattnera4176522005-10-30 18:54:27 +0000300 (*I)->contains(SrcReg)) {
301 TRC = *I;
302 break;
303 }
304 assert(TRC && "Couldn't find register class for reg copy!");
Jim Laskeyb6d4c2c2005-09-30 19:15:27 +0000305
Chris Lattnera4176522005-10-30 18:54:27 +0000306 // Create the reg, emit the copy.
307 VRBase = RegMap->createVirtualRegister(TRC);
308 }
Evan Chenga9c20912006-01-21 02:32:06 +0000309 MRI->copyRegToReg(*BB, BB->end(), VRBase, SrcReg, TRC);
Jim Laskeyb6d4c2c2005-09-30 19:15:27 +0000310 break;
311 }
Chris Lattneracc43bf2006-01-26 23:28:04 +0000312 case ISD::INLINEASM: {
313 unsigned NumOps = Node->getNumOperands();
314 if (Node->getOperand(NumOps-1).getValueType() == MVT::Flag)
315 --NumOps; // Ignore the flag operand.
316
317 // Create the inline asm machine instruction.
318 MachineInstr *MI =
319 new MachineInstr(BB, TargetInstrInfo::INLINEASM, (NumOps-2)/2+1);
320
321 // Add the asm string as an external symbol operand.
322 const char *AsmStr =
323 cast<ExternalSymbolSDNode>(Node->getOperand(1))->getSymbol();
324 MI->addExternalSymbolOperand(AsmStr, false);
325
326 // Add all of the operand registers to the instruction.
Chris Lattnerc3a9f8d2006-02-23 19:21:04 +0000327 for (unsigned i = 2; i != NumOps;) {
328 unsigned Flags = cast<ConstantSDNode>(Node->getOperand(i))->getValue();
Chris Lattnerfd6d2822006-02-24 19:18:20 +0000329 unsigned NumVals = Flags >> 3;
Chris Lattnerc3a9f8d2006-02-23 19:21:04 +0000330
Chris Lattnerdaf6bc62006-02-24 19:50:58 +0000331 MI->addZeroExtImm64Operand(Flags);
Chris Lattnerc3a9f8d2006-02-23 19:21:04 +0000332 ++i; // Skip the ID value.
333
334 switch (Flags & 7) {
Chris Lattneracc43bf2006-01-26 23:28:04 +0000335 default: assert(0 && "Bad flags!");
Chris Lattnerc3a9f8d2006-02-23 19:21:04 +0000336 case 1: // Use of register.
Chris Lattnerfd6d2822006-02-24 19:18:20 +0000337 for (; NumVals; --NumVals, ++i) {
Chris Lattnerc3a9f8d2006-02-23 19:21:04 +0000338 unsigned Reg = cast<RegisterSDNode>(Node->getOperand(i))->getReg();
339 MI->addMachineRegOperand(Reg, MachineOperand::Use);
340 }
Chris Lattnerdc19b702006-02-04 02:26:14 +0000341 break;
Chris Lattnerc3a9f8d2006-02-23 19:21:04 +0000342 case 2: // Def of register.
Chris Lattnerfd6d2822006-02-24 19:18:20 +0000343 for (; NumVals; --NumVals, ++i) {
Chris Lattnerc3a9f8d2006-02-23 19:21:04 +0000344 unsigned Reg = cast<RegisterSDNode>(Node->getOperand(i))->getReg();
345 MI->addMachineRegOperand(Reg, MachineOperand::Def);
346 }
Chris Lattnerdc19b702006-02-04 02:26:14 +0000347 break;
Chris Lattnerdc19b702006-02-04 02:26:14 +0000348 case 3: { // Immediate.
Chris Lattnerfd6d2822006-02-24 19:18:20 +0000349 assert(NumVals == 1 && "Unknown immediate value!");
Chris Lattnerdc19b702006-02-04 02:26:14 +0000350 uint64_t Val = cast<ConstantSDNode>(Node->getOperand(i))->getValue();
351 MI->addZeroExtImm64Operand(Val);
Chris Lattnerc3a9f8d2006-02-23 19:21:04 +0000352 ++i;
Chris Lattnerdc19b702006-02-04 02:26:14 +0000353 break;
354 }
Chris Lattnerfd6d2822006-02-24 19:18:20 +0000355 case 4: // Addressing mode.
356 // The addressing mode has been selected, just add all of the
357 // operands to the machine instruction.
358 for (; NumVals; --NumVals, ++i)
359 AddOperand(MI, Node->getOperand(i), 0, 0);
360 break;
Chris Lattnerdc19b702006-02-04 02:26:14 +0000361 }
Chris Lattneracc43bf2006-01-26 23:28:04 +0000362 }
363 break;
364 }
Jim Laskeyb6d4c2c2005-09-30 19:15:27 +0000365 }
366 }
367
368 assert(NI->VRBase == 0 && "Node emitted out of order - early");
369 NI->VRBase = VRBase;
370}
371
Evan Cheng4ef10862006-01-23 07:01:07 +0000372/// EmitAll - Emit all nodes in schedule sorted order.
373///
374void ScheduleDAG::EmitAll() {
375 // For each node in the ordering
376 for (unsigned i = 0, N = Ordering.size(); i < N; i++) {
377 // Get the scheduling info
378 NodeInfo *NI = Ordering[i];
379 if (NI->isInGroup()) {
380 NodeGroupIterator NGI(Ordering[i]);
381 while (NodeInfo *NI = NGI.next()) EmitNode(NI);
382 } else {
383 EmitNode(NI);
384 }
385 }
386}
387
388/// isFlagDefiner - Returns true if the node defines a flag result.
389static bool isFlagDefiner(SDNode *A) {
390 unsigned N = A->getNumValues();
391 return N && A->getValueType(N - 1) == MVT::Flag;
392}
393
394/// isFlagUser - Returns true if the node uses a flag result.
395///
396static bool isFlagUser(SDNode *A) {
397 unsigned N = A->getNumOperands();
398 return N && A->getOperand(N - 1).getValueType() == MVT::Flag;
399}
400
401/// printNI - Print node info.
402///
403void ScheduleDAG::printNI(std::ostream &O, NodeInfo *NI) const {
404#ifndef NDEBUG
405 SDNode *Node = NI->Node;
406 O << " "
407 << std::hex << Node << std::dec
408 << ", Lat=" << NI->Latency
409 << ", Slot=" << NI->Slot
410 << ", ARITY=(" << Node->getNumOperands() << ","
411 << Node->getNumValues() << ")"
412 << " " << Node->getOperationName(&DAG);
413 if (isFlagDefiner(Node)) O << "<#";
414 if (isFlagUser(Node)) O << ">#";
415#endif
416}
417
418/// printChanges - Hilight changes in order caused by scheduling.
419///
420void ScheduleDAG::printChanges(unsigned Index) const {
421#ifndef NDEBUG
422 // Get the ordered node count
423 unsigned N = Ordering.size();
424 // Determine if any changes
425 unsigned i = 0;
426 for (; i < N; i++) {
427 NodeInfo *NI = Ordering[i];
428 if (NI->Preorder != i) break;
429 }
430
431 if (i < N) {
432 std::cerr << Index << ". New Ordering\n";
433
434 for (i = 0; i < N; i++) {
435 NodeInfo *NI = Ordering[i];
436 std::cerr << " " << NI->Preorder << ". ";
437 printNI(std::cerr, NI);
438 std::cerr << "\n";
439 if (NI->isGroupDominator()) {
440 NodeGroup *Group = NI->Group;
441 for (NIIterator NII = Group->group_begin(), E = Group->group_end();
442 NII != E; NII++) {
443 std::cerr << " ";
444 printNI(std::cerr, *NII);
445 std::cerr << "\n";
446 }
447 }
448 }
449 } else {
450 std::cerr << Index << ". No Changes\n";
451 }
452#endif
453}
454
455/// print - Print ordering to specified output stream.
456///
457void ScheduleDAG::print(std::ostream &O) const {
458#ifndef NDEBUG
459 using namespace std;
460 O << "Ordering\n";
461 for (unsigned i = 0, N = Ordering.size(); i < N; i++) {
462 NodeInfo *NI = Ordering[i];
463 printNI(O, NI);
464 O << "\n";
465 if (NI->isGroupDominator()) {
466 NodeGroup *Group = NI->Group;
467 for (NIIterator NII = Group->group_begin(), E = Group->group_end();
468 NII != E; NII++) {
469 O << " ";
470 printNI(O, *NII);
471 O << "\n";
472 }
473 }
474 }
475#endif
476}
477
Evan Chenga9c20912006-01-21 02:32:06 +0000478void ScheduleDAG::dump(const char *tag) const {
479 std::cerr << tag; dump();
Jim Laskeyfab66f62005-10-12 18:29:35 +0000480}
481
Evan Chenga9c20912006-01-21 02:32:06 +0000482void ScheduleDAG::dump() const {
Jim Laskeye6b90fb2005-09-26 21:57:04 +0000483 print(std::cerr);
484}
Jim Laskeye6b90fb2005-09-26 21:57:04 +0000485
Evan Chenga9c20912006-01-21 02:32:06 +0000486/// Run - perform scheduling.
487///
488MachineBasicBlock *ScheduleDAG::Run() {
489 TII = TM.getInstrInfo();
490 MRI = TM.getRegisterInfo();
491 RegMap = BB->getParent()->getSSARegMap();
492 ConstPool = BB->getParent()->getConstantPool();
Evan Cheng4ef10862006-01-23 07:01:07 +0000493
494 // Number the nodes
495 NodeCount = std::distance(DAG.allnodes_begin(), DAG.allnodes_end());
496 // Set up minimum info for scheduling
497 PrepareNodeInfo();
498 // Construct node groups for flagged nodes
499 IdentifyGroups();
500
Evan Chenga9c20912006-01-21 02:32:06 +0000501 Schedule();
502 return BB;
Chris Lattnerd32b2362005-08-18 18:45:24 +0000503}
Evan Cheng4ef10862006-01-23 07:01:07 +0000504
505
506/// CountInternalUses - Returns the number of edges between the two nodes.
507///
508static unsigned CountInternalUses(NodeInfo *D, NodeInfo *U) {
509 unsigned N = 0;
510 for (unsigned M = U->Node->getNumOperands(); 0 < M--;) {
511 SDOperand Op = U->Node->getOperand(M);
512 if (Op.Val == D->Node) N++;
513 }
514
515 return N;
516}
517
518//===----------------------------------------------------------------------===//
519/// Add - Adds a definer and user pair to a node group.
520///
Evan Chengcccf1232006-02-04 06:49:00 +0000521void ScheduleDAG::AddToGroup(NodeInfo *D, NodeInfo *U) {
Evan Cheng4ef10862006-01-23 07:01:07 +0000522 // Get current groups
523 NodeGroup *DGroup = D->Group;
524 NodeGroup *UGroup = U->Group;
525 // If both are members of groups
526 if (DGroup && UGroup) {
527 // There may have been another edge connecting
528 if (DGroup == UGroup) return;
529 // Add the pending users count
530 DGroup->addPending(UGroup->getPending());
531 // For each member of the users group
532 NodeGroupIterator UNGI(U);
533 while (NodeInfo *UNI = UNGI.next() ) {
534 // Change the group
535 UNI->Group = DGroup;
536 // For each member of the definers group
537 NodeGroupIterator DNGI(D);
538 while (NodeInfo *DNI = DNGI.next() ) {
539 // Remove internal edges
540 DGroup->addPending(-CountInternalUses(DNI, UNI));
541 }
542 }
543 // Merge the two lists
544 DGroup->group_insert(DGroup->group_end(),
545 UGroup->group_begin(), UGroup->group_end());
546 } else if (DGroup) {
547 // Make user member of definers group
548 U->Group = DGroup;
549 // Add users uses to definers group pending
550 DGroup->addPending(U->Node->use_size());
551 // For each member of the definers group
552 NodeGroupIterator DNGI(D);
553 while (NodeInfo *DNI = DNGI.next() ) {
554 // Remove internal edges
555 DGroup->addPending(-CountInternalUses(DNI, U));
556 }
557 DGroup->group_push_back(U);
558 } else if (UGroup) {
559 // Make definer member of users group
560 D->Group = UGroup;
561 // Add definers uses to users group pending
562 UGroup->addPending(D->Node->use_size());
563 // For each member of the users group
564 NodeGroupIterator UNGI(U);
565 while (NodeInfo *UNI = UNGI.next() ) {
566 // Remove internal edges
567 UGroup->addPending(-CountInternalUses(D, UNI));
568 }
569 UGroup->group_insert(UGroup->group_begin(), D);
570 } else {
571 D->Group = U->Group = DGroup = new NodeGroup();
572 DGroup->addPending(D->Node->use_size() + U->Node->use_size() -
573 CountInternalUses(D, U));
574 DGroup->group_push_back(D);
575 DGroup->group_push_back(U);
Evan Chengcccf1232006-02-04 06:49:00 +0000576
577 if (HeadNG == NULL)
578 HeadNG = DGroup;
579 if (TailNG != NULL)
580 TailNG->Next = DGroup;
581 TailNG = DGroup;
Evan Cheng4ef10862006-01-23 07:01:07 +0000582 }
583}