blob: 157487056909ae4a168078460b5c049111518fe7 [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"
26#include <iostream>
Chris Lattnerd32b2362005-08-18 18:45:24 +000027using namespace llvm;
28
Jim Laskeye6b90fb2005-09-26 21:57:04 +000029
30/// CountResults - The results of target nodes have register or immediate
31/// operands first, then an optional chain, and optional flag operands (which do
32/// not go into the machine instrs.)
Evan Chenga9c20912006-01-21 02:32:06 +000033static unsigned CountResults(SDNode *Node) {
Jim Laskeyb6d4c2c2005-09-30 19:15:27 +000034 unsigned N = Node->getNumValues();
35 while (N && Node->getValueType(N - 1) == MVT::Flag)
Jim Laskeye6b90fb2005-09-26 21:57:04 +000036 --N;
Jim Laskeyb6d4c2c2005-09-30 19:15:27 +000037 if (N && Node->getValueType(N - 1) == MVT::Other)
Jim Laskeye6b90fb2005-09-26 21:57:04 +000038 --N; // Skip over chain result.
39 return N;
40}
41
42/// CountOperands The inputs to target nodes have any actual inputs first,
43/// followed by an optional chain operand, then flag operands. Compute the
44/// number of actual operands that will go into the machine instr.
Evan Chenga9c20912006-01-21 02:32:06 +000045static unsigned CountOperands(SDNode *Node) {
Jim Laskeyb6d4c2c2005-09-30 19:15:27 +000046 unsigned N = Node->getNumOperands();
47 while (N && Node->getOperand(N - 1).getValueType() == MVT::Flag)
Jim Laskeye6b90fb2005-09-26 21:57:04 +000048 --N;
Jim Laskeyb6d4c2c2005-09-30 19:15:27 +000049 if (N && Node->getOperand(N - 1).getValueType() == MVT::Other)
Jim Laskeye6b90fb2005-09-26 21:57:04 +000050 --N; // Ignore chain if it exists.
51 return N;
52}
53
Evan Cheng4ef10862006-01-23 07:01:07 +000054/// PrepareNodeInfo - Set up the basic minimum node info for scheduling.
55///
56void ScheduleDAG::PrepareNodeInfo() {
57 // Allocate node information
58 Info = new NodeInfo[NodeCount];
59
60 unsigned i = 0;
61 for (SelectionDAG::allnodes_iterator I = DAG.allnodes_begin(),
62 E = DAG.allnodes_end(); I != E; ++I, ++i) {
63 // Fast reference to node schedule info
64 NodeInfo* NI = &Info[i];
65 // Set up map
66 Map[I] = NI;
67 // Set node
68 NI->Node = I;
69 // Set pending visit count
70 NI->setPending(I->use_size());
71 }
72}
73
74/// IdentifyGroups - Put flagged nodes into groups.
75///
76void ScheduleDAG::IdentifyGroups() {
77 for (unsigned i = 0, N = NodeCount; i < N; i++) {
78 NodeInfo* NI = &Info[i];
79 SDNode *Node = NI->Node;
80
81 // For each operand (in reverse to only look at flags)
82 for (unsigned N = Node->getNumOperands(); 0 < N--;) {
83 // Get operand
84 SDOperand Op = Node->getOperand(N);
85 // No more flags to walk
86 if (Op.getValueType() != MVT::Flag) break;
87 // Add to node group
Evan Chengcccf1232006-02-04 06:49:00 +000088 AddToGroup(getNI(Op.Val), NI);
Evan Chenge0a58322006-01-25 09:13:41 +000089 // Let everyone else know
Evan Cheng4ef10862006-01-23 07:01:07 +000090 HasGroups = true;
91 }
92 }
93}
94
95static unsigned CreateVirtualRegisters(MachineInstr *MI,
96 unsigned NumResults,
97 SSARegMap *RegMap,
98 const TargetInstrDescriptor &II) {
Jim Laskeye6b90fb2005-09-26 21:57:04 +000099 // Create the result registers for this node and add the result regs to
100 // the machine instruction.
101 const TargetOperandInfo *OpInfo = II.OpInfo;
102 unsigned ResultReg = RegMap->createVirtualRegister(OpInfo[0].RegClass);
103 MI->addRegOperand(ResultReg, MachineOperand::Def);
104 for (unsigned i = 1; i != NumResults; ++i) {
105 assert(OpInfo[i].RegClass && "Isn't a register operand!");
Chris Lattner505277a2005-10-01 07:45:09 +0000106 MI->addRegOperand(RegMap->createVirtualRegister(OpInfo[i].RegClass),
Jim Laskeye6b90fb2005-09-26 21:57:04 +0000107 MachineOperand::Def);
108 }
109 return ResultReg;
110}
111
Jim Laskeyb6d4c2c2005-09-30 19:15:27 +0000112/// EmitNode - Generate machine code for an node and needed dependencies.
Jim Laskeye6b90fb2005-09-26 21:57:04 +0000113///
Evan Chenga9c20912006-01-21 02:32:06 +0000114void ScheduleDAG::EmitNode(NodeInfo *NI) {
Jim Laskeyb6d4c2c2005-09-30 19:15:27 +0000115 unsigned VRBase = 0; // First virtual register for node
116 SDNode *Node = NI->Node;
Chris Lattner2d973e42005-08-18 20:07:59 +0000117
Jim Laskeyb6d4c2c2005-09-30 19:15:27 +0000118 // If machine instruction
119 if (Node->isTargetOpcode()) {
120 unsigned Opc = Node->getTargetOpcode();
Evan Chenga9c20912006-01-21 02:32:06 +0000121 const TargetInstrDescriptor &II = TII->get(Opc);
Chris Lattner2d973e42005-08-18 20:07:59 +0000122
Jim Laskeyb6d4c2c2005-09-30 19:15:27 +0000123 unsigned NumResults = CountResults(Node);
124 unsigned NodeOperands = CountOperands(Node);
Jim Laskeye6b90fb2005-09-26 21:57:04 +0000125 unsigned NumMIOperands = NodeOperands + NumResults;
Chris Lattnerda8abb02005-09-01 18:44:10 +0000126#ifndef NDEBUG
Chris Lattner14b392a2005-08-24 22:02:41 +0000127 assert((unsigned(II.numOperands) == NumMIOperands || II.numOperands == -1)&&
Chris Lattner2d973e42005-08-18 20:07:59 +0000128 "#operands for dag node doesn't match .td file!");
Chris Lattnerca6aa2f2005-08-19 01:01:34 +0000129#endif
Chris Lattner2d973e42005-08-18 20:07:59 +0000130
131 // Create the new machine instruction.
Chris Lattner14b392a2005-08-24 22:02:41 +0000132 MachineInstr *MI = new MachineInstr(Opc, NumMIOperands, true, true);
Chris Lattner2d973e42005-08-18 20:07:59 +0000133
134 // Add result register values for things that are defined by this
135 // instruction.
Chris Lattnera4176522005-10-30 18:54:27 +0000136
137 // If the node is only used by a CopyToReg and the dest reg is a vreg, use
138 // the CopyToReg'd destination register instead of creating a new vreg.
139 if (NumResults == 1) {
140 for (SDNode::use_iterator UI = Node->use_begin(), E = Node->use_end();
141 UI != E; ++UI) {
142 SDNode *Use = *UI;
143 if (Use->getOpcode() == ISD::CopyToReg &&
144 Use->getOperand(2).Val == Node) {
145 unsigned Reg = cast<RegisterSDNode>(Use->getOperand(1))->getReg();
146 if (MRegisterInfo::isVirtualRegister(Reg)) {
147 VRBase = Reg;
148 MI->addRegOperand(Reg, MachineOperand::Def);
149 break;
150 }
151 }
152 }
153 }
154
155 // Otherwise, create new virtual registers.
156 if (NumResults && VRBase == 0)
Evan Cheng4ef10862006-01-23 07:01:07 +0000157 VRBase = CreateVirtualRegisters(MI, NumResults, RegMap, II);
Jim Laskeyb6d4c2c2005-09-30 19:15:27 +0000158
159 // Emit all of the actual operands of this instruction, adding them to the
160 // instruction as appropriate.
161 for (unsigned i = 0; i != NodeOperands; ++i) {
162 if (Node->getOperand(i).isTargetOpcode()) {
163 // Note that this case is redundant with the final else block, but we
164 // include it because it is the most common and it makes the logic
165 // simpler here.
166 assert(Node->getOperand(i).getValueType() != MVT::Other &&
167 Node->getOperand(i).getValueType() != MVT::Flag &&
168 "Chain and flag operands should occur at end of operand list!");
Chris Lattner505277a2005-10-01 07:45:09 +0000169
170 // Get/emit the operand.
171 unsigned VReg = getVR(Node->getOperand(i));
172 MI->addRegOperand(VReg, MachineOperand::Use);
Jim Laskeyb6d4c2c2005-09-30 19:15:27 +0000173
Chris Lattner505277a2005-10-01 07:45:09 +0000174 // Verify that it is right.
175 assert(MRegisterInfo::isVirtualRegister(VReg) && "Not a vreg?");
176 assert(II.OpInfo[i+NumResults].RegClass &&
177 "Don't have operand info for this instruction!");
178 assert(RegMap->getRegClass(VReg) == II.OpInfo[i+NumResults].RegClass &&
179 "Register class of operand and regclass of use don't agree!");
Jim Laskeyb6d4c2c2005-09-30 19:15:27 +0000180 } else if (ConstantSDNode *C =
181 dyn_cast<ConstantSDNode>(Node->getOperand(i))) {
182 MI->addZeroExtImm64Operand(C->getValue());
183 } else if (RegisterSDNode*R =
184 dyn_cast<RegisterSDNode>(Node->getOperand(i))) {
185 MI->addRegOperand(R->getReg(), MachineOperand::Use);
186 } else if (GlobalAddressSDNode *TGA =
187 dyn_cast<GlobalAddressSDNode>(Node->getOperand(i))) {
Evan Cheng61ca74b2005-11-30 02:04:11 +0000188 MI->addGlobalAddressOperand(TGA->getGlobal(), false, TGA->getOffset());
Jim Laskeyb6d4c2c2005-09-30 19:15:27 +0000189 } else if (BasicBlockSDNode *BB =
190 dyn_cast<BasicBlockSDNode>(Node->getOperand(i))) {
191 MI->addMachineBasicBlockOperand(BB->getBasicBlock());
192 } else if (FrameIndexSDNode *FI =
193 dyn_cast<FrameIndexSDNode>(Node->getOperand(i))) {
194 MI->addFrameIndexOperand(FI->getIndex());
195 } else if (ConstantPoolSDNode *CP =
196 dyn_cast<ConstantPoolSDNode>(Node->getOperand(i))) {
Evan Chengb8973bd2006-01-31 22:23:14 +0000197 unsigned Idx = ConstPool->getConstantPoolIndex(CP->get(),
198 CP->getAlignment());
Jim Laskeyb6d4c2c2005-09-30 19:15:27 +0000199 MI->addConstantPoolIndexOperand(Idx);
200 } else if (ExternalSymbolSDNode *ES =
201 dyn_cast<ExternalSymbolSDNode>(Node->getOperand(i))) {
202 MI->addExternalSymbolOperand(ES->getSymbol(), false);
203 } else {
204 assert(Node->getOperand(i).getValueType() != MVT::Other &&
205 Node->getOperand(i).getValueType() != MVT::Flag &&
206 "Chain and flag operands should occur at end of operand list!");
Chris Lattner505277a2005-10-01 07:45:09 +0000207 unsigned VReg = getVR(Node->getOperand(i));
208 MI->addRegOperand(VReg, MachineOperand::Use);
209
210 // Verify that it is right.
211 assert(MRegisterInfo::isVirtualRegister(VReg) && "Not a vreg?");
212 assert(II.OpInfo[i+NumResults].RegClass &&
213 "Don't have operand info for this instruction!");
214 assert(RegMap->getRegClass(VReg) == II.OpInfo[i+NumResults].RegClass &&
215 "Register class of operand and regclass of use don't agree!");
Jim Laskeyb6d4c2c2005-09-30 19:15:27 +0000216 }
217 }
218
219 // Now that we have emitted all operands, emit this instruction itself.
220 if ((II.Flags & M_USES_CUSTOM_DAG_SCHED_INSERTION) == 0) {
221 BB->insert(BB->end(), MI);
222 } else {
223 // Insert this instruction into the end of the basic block, potentially
224 // taking some custom action.
225 BB = DAG.getTargetLoweringInfo().InsertAtEndOfBasicBlock(MI, BB);
226 }
227 } else {
228 switch (Node->getOpcode()) {
229 default:
230 Node->dump();
231 assert(0 && "This target-independent node should have been selected!");
232 case ISD::EntryToken: // fall thru
233 case ISD::TokenFactor:
234 break;
235 case ISD::CopyToReg: {
Chris Lattnera4176522005-10-30 18:54:27 +0000236 unsigned InReg = getVR(Node->getOperand(2));
237 unsigned DestReg = cast<RegisterSDNode>(Node->getOperand(1))->getReg();
238 if (InReg != DestReg) // Coallesced away the copy?
Evan Chenga9c20912006-01-21 02:32:06 +0000239 MRI->copyRegToReg(*BB, BB->end(), DestReg, InReg,
240 RegMap->getRegClass(InReg));
Jim Laskeyb6d4c2c2005-09-30 19:15:27 +0000241 break;
242 }
243 case ISD::CopyFromReg: {
244 unsigned SrcReg = cast<RegisterSDNode>(Node->getOperand(1))->getReg();
Chris Lattner089c25c2005-10-09 05:58:56 +0000245 if (MRegisterInfo::isVirtualRegister(SrcReg)) {
246 VRBase = SrcReg; // Just use the input register directly!
247 break;
248 }
249
Chris Lattnera4176522005-10-30 18:54:27 +0000250 // If the node is only used by a CopyToReg and the dest reg is a vreg, use
251 // the CopyToReg'd destination register instead of creating a new vreg.
252 for (SDNode::use_iterator UI = Node->use_begin(), E = Node->use_end();
253 UI != E; ++UI) {
254 SDNode *Use = *UI;
255 if (Use->getOpcode() == ISD::CopyToReg &&
256 Use->getOperand(2).Val == Node) {
257 unsigned DestReg = cast<RegisterSDNode>(Use->getOperand(1))->getReg();
258 if (MRegisterInfo::isVirtualRegister(DestReg)) {
259 VRBase = DestReg;
260 break;
261 }
262 }
263 }
264
Jim Laskeyb6d4c2c2005-09-30 19:15:27 +0000265 // Figure out the register class to create for the destreg.
266 const TargetRegisterClass *TRC = 0;
Chris Lattnera4176522005-10-30 18:54:27 +0000267 if (VRBase) {
268 TRC = RegMap->getRegClass(VRBase);
269 } else {
Chris Lattner089c25c2005-10-09 05:58:56 +0000270
Chris Lattnera4176522005-10-30 18:54:27 +0000271 // Pick the register class of the right type that contains this physreg.
Evan Chenga9c20912006-01-21 02:32:06 +0000272 for (MRegisterInfo::regclass_iterator I = MRI->regclass_begin(),
273 E = MRI->regclass_end(); I != E; ++I)
Nate Begeman6510b222005-12-01 04:51:06 +0000274 if ((*I)->hasType(Node->getValueType(0)) &&
Chris Lattnera4176522005-10-30 18:54:27 +0000275 (*I)->contains(SrcReg)) {
276 TRC = *I;
277 break;
278 }
279 assert(TRC && "Couldn't find register class for reg copy!");
Jim Laskeyb6d4c2c2005-09-30 19:15:27 +0000280
Chris Lattnera4176522005-10-30 18:54:27 +0000281 // Create the reg, emit the copy.
282 VRBase = RegMap->createVirtualRegister(TRC);
283 }
Evan Chenga9c20912006-01-21 02:32:06 +0000284 MRI->copyRegToReg(*BB, BB->end(), VRBase, SrcReg, TRC);
Jim Laskeyb6d4c2c2005-09-30 19:15:27 +0000285 break;
286 }
Chris Lattneracc43bf2006-01-26 23:28:04 +0000287 case ISD::INLINEASM: {
288 unsigned NumOps = Node->getNumOperands();
289 if (Node->getOperand(NumOps-1).getValueType() == MVT::Flag)
290 --NumOps; // Ignore the flag operand.
291
292 // Create the inline asm machine instruction.
293 MachineInstr *MI =
294 new MachineInstr(BB, TargetInstrInfo::INLINEASM, (NumOps-2)/2+1);
295
296 // Add the asm string as an external symbol operand.
297 const char *AsmStr =
298 cast<ExternalSymbolSDNode>(Node->getOperand(1))->getSymbol();
299 MI->addExternalSymbolOperand(AsmStr, false);
300
301 // Add all of the operand registers to the instruction.
302 for (unsigned i = 2; i != NumOps; i += 2) {
Chris Lattner6656dd12006-01-31 02:03:41 +0000303 unsigned Flags =cast<ConstantSDNode>(Node->getOperand(i+1))->getValue();
Chris Lattneracc43bf2006-01-26 23:28:04 +0000304 switch (Flags) {
305 default: assert(0 && "Bad flags!");
Chris Lattnerdc19b702006-02-04 02:26:14 +0000306 case 1: { // Use of register.
307 unsigned Reg = cast<RegisterSDNode>(Node->getOperand(i))->getReg();
308 MI->addMachineRegOperand(Reg, MachineOperand::Use);
309 break;
Chris Lattneracc43bf2006-01-26 23:28:04 +0000310 }
Chris Lattnerdc19b702006-02-04 02:26:14 +0000311 case 2: { // Def of register.
312 unsigned Reg = cast<RegisterSDNode>(Node->getOperand(i))->getReg();
313 MI->addMachineRegOperand(Reg, MachineOperand::Def);
314 break;
315 }
316 case 3: { // Immediate.
317 uint64_t Val = cast<ConstantSDNode>(Node->getOperand(i))->getValue();
318 MI->addZeroExtImm64Operand(Val);
319 break;
320 }
321 }
Chris Lattneracc43bf2006-01-26 23:28:04 +0000322 }
323 break;
324 }
Jim Laskeyb6d4c2c2005-09-30 19:15:27 +0000325 }
326 }
327
328 assert(NI->VRBase == 0 && "Node emitted out of order - early");
329 NI->VRBase = VRBase;
330}
331
Evan Cheng4ef10862006-01-23 07:01:07 +0000332/// EmitAll - Emit all nodes in schedule sorted order.
333///
334void ScheduleDAG::EmitAll() {
335 // For each node in the ordering
336 for (unsigned i = 0, N = Ordering.size(); i < N; i++) {
337 // Get the scheduling info
338 NodeInfo *NI = Ordering[i];
339 if (NI->isInGroup()) {
340 NodeGroupIterator NGI(Ordering[i]);
341 while (NodeInfo *NI = NGI.next()) EmitNode(NI);
342 } else {
343 EmitNode(NI);
344 }
345 }
346}
347
348/// isFlagDefiner - Returns true if the node defines a flag result.
349static bool isFlagDefiner(SDNode *A) {
350 unsigned N = A->getNumValues();
351 return N && A->getValueType(N - 1) == MVT::Flag;
352}
353
354/// isFlagUser - Returns true if the node uses a flag result.
355///
356static bool isFlagUser(SDNode *A) {
357 unsigned N = A->getNumOperands();
358 return N && A->getOperand(N - 1).getValueType() == MVT::Flag;
359}
360
361/// printNI - Print node info.
362///
363void ScheduleDAG::printNI(std::ostream &O, NodeInfo *NI) const {
364#ifndef NDEBUG
365 SDNode *Node = NI->Node;
366 O << " "
367 << std::hex << Node << std::dec
368 << ", Lat=" << NI->Latency
369 << ", Slot=" << NI->Slot
370 << ", ARITY=(" << Node->getNumOperands() << ","
371 << Node->getNumValues() << ")"
372 << " " << Node->getOperationName(&DAG);
373 if (isFlagDefiner(Node)) O << "<#";
374 if (isFlagUser(Node)) O << ">#";
375#endif
376}
377
378/// printChanges - Hilight changes in order caused by scheduling.
379///
380void ScheduleDAG::printChanges(unsigned Index) const {
381#ifndef NDEBUG
382 // Get the ordered node count
383 unsigned N = Ordering.size();
384 // Determine if any changes
385 unsigned i = 0;
386 for (; i < N; i++) {
387 NodeInfo *NI = Ordering[i];
388 if (NI->Preorder != i) break;
389 }
390
391 if (i < N) {
392 std::cerr << Index << ". New Ordering\n";
393
394 for (i = 0; i < N; i++) {
395 NodeInfo *NI = Ordering[i];
396 std::cerr << " " << NI->Preorder << ". ";
397 printNI(std::cerr, NI);
398 std::cerr << "\n";
399 if (NI->isGroupDominator()) {
400 NodeGroup *Group = NI->Group;
401 for (NIIterator NII = Group->group_begin(), E = Group->group_end();
402 NII != E; NII++) {
403 std::cerr << " ";
404 printNI(std::cerr, *NII);
405 std::cerr << "\n";
406 }
407 }
408 }
409 } else {
410 std::cerr << Index << ". No Changes\n";
411 }
412#endif
413}
414
415/// print - Print ordering to specified output stream.
416///
417void ScheduleDAG::print(std::ostream &O) const {
418#ifndef NDEBUG
419 using namespace std;
420 O << "Ordering\n";
421 for (unsigned i = 0, N = Ordering.size(); i < N; i++) {
422 NodeInfo *NI = Ordering[i];
423 printNI(O, NI);
424 O << "\n";
425 if (NI->isGroupDominator()) {
426 NodeGroup *Group = NI->Group;
427 for (NIIterator NII = Group->group_begin(), E = Group->group_end();
428 NII != E; NII++) {
429 O << " ";
430 printNI(O, *NII);
431 O << "\n";
432 }
433 }
434 }
435#endif
436}
437
Evan Chenga9c20912006-01-21 02:32:06 +0000438void ScheduleDAG::dump(const char *tag) const {
439 std::cerr << tag; dump();
Jim Laskeyfab66f62005-10-12 18:29:35 +0000440}
441
Evan Chenga9c20912006-01-21 02:32:06 +0000442void ScheduleDAG::dump() const {
Jim Laskeye6b90fb2005-09-26 21:57:04 +0000443 print(std::cerr);
444}
Jim Laskeye6b90fb2005-09-26 21:57:04 +0000445
Evan Chenga9c20912006-01-21 02:32:06 +0000446/// Run - perform scheduling.
447///
448MachineBasicBlock *ScheduleDAG::Run() {
449 TII = TM.getInstrInfo();
450 MRI = TM.getRegisterInfo();
451 RegMap = BB->getParent()->getSSARegMap();
452 ConstPool = BB->getParent()->getConstantPool();
Evan Cheng4ef10862006-01-23 07:01:07 +0000453
454 // Number the nodes
455 NodeCount = std::distance(DAG.allnodes_begin(), DAG.allnodes_end());
456 // Set up minimum info for scheduling
457 PrepareNodeInfo();
458 // Construct node groups for flagged nodes
459 IdentifyGroups();
460
Evan Chenga9c20912006-01-21 02:32:06 +0000461 Schedule();
462 return BB;
Chris Lattnerd32b2362005-08-18 18:45:24 +0000463}
Evan Cheng4ef10862006-01-23 07:01:07 +0000464
465
466/// CountInternalUses - Returns the number of edges between the two nodes.
467///
468static unsigned CountInternalUses(NodeInfo *D, NodeInfo *U) {
469 unsigned N = 0;
470 for (unsigned M = U->Node->getNumOperands(); 0 < M--;) {
471 SDOperand Op = U->Node->getOperand(M);
472 if (Op.Val == D->Node) N++;
473 }
474
475 return N;
476}
477
478//===----------------------------------------------------------------------===//
479/// Add - Adds a definer and user pair to a node group.
480///
Evan Chengcccf1232006-02-04 06:49:00 +0000481void ScheduleDAG::AddToGroup(NodeInfo *D, NodeInfo *U) {
Evan Cheng4ef10862006-01-23 07:01:07 +0000482 // Get current groups
483 NodeGroup *DGroup = D->Group;
484 NodeGroup *UGroup = U->Group;
485 // If both are members of groups
486 if (DGroup && UGroup) {
487 // There may have been another edge connecting
488 if (DGroup == UGroup) return;
489 // Add the pending users count
490 DGroup->addPending(UGroup->getPending());
491 // For each member of the users group
492 NodeGroupIterator UNGI(U);
493 while (NodeInfo *UNI = UNGI.next() ) {
494 // Change the group
495 UNI->Group = DGroup;
496 // For each member of the definers group
497 NodeGroupIterator DNGI(D);
498 while (NodeInfo *DNI = DNGI.next() ) {
499 // Remove internal edges
500 DGroup->addPending(-CountInternalUses(DNI, UNI));
501 }
502 }
503 // Merge the two lists
504 DGroup->group_insert(DGroup->group_end(),
505 UGroup->group_begin(), UGroup->group_end());
506 } else if (DGroup) {
507 // Make user member of definers group
508 U->Group = DGroup;
509 // Add users uses to definers group pending
510 DGroup->addPending(U->Node->use_size());
511 // For each member of the definers group
512 NodeGroupIterator DNGI(D);
513 while (NodeInfo *DNI = DNGI.next() ) {
514 // Remove internal edges
515 DGroup->addPending(-CountInternalUses(DNI, U));
516 }
517 DGroup->group_push_back(U);
518 } else if (UGroup) {
519 // Make definer member of users group
520 D->Group = UGroup;
521 // Add definers uses to users group pending
522 UGroup->addPending(D->Node->use_size());
523 // For each member of the users group
524 NodeGroupIterator UNGI(U);
525 while (NodeInfo *UNI = UNGI.next() ) {
526 // Remove internal edges
527 UGroup->addPending(-CountInternalUses(D, UNI));
528 }
529 UGroup->group_insert(UGroup->group_begin(), D);
530 } else {
531 D->Group = U->Group = DGroup = new NodeGroup();
532 DGroup->addPending(D->Node->use_size() + U->Node->use_size() -
533 CountInternalUses(D, U));
534 DGroup->group_push_back(D);
535 DGroup->group_push_back(U);
Evan Chengcccf1232006-02-04 06:49:00 +0000536
537 if (HeadNG == NULL)
538 HeadNG = DGroup;
539 if (TailNG != NULL)
540 TailNG->Next = DGroup;
541 TailNG = DGroup;
Evan Cheng4ef10862006-01-23 07:01:07 +0000542 }
543}