blob: ff4ae335f2de19871951577d30069bad9e814d39 [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
Jim Laskeyb6d4c2c2005-09-30 19:15:27 +0000113/// EmitNode - Generate machine code for an node and needed dependencies.
Jim Laskeye6b90fb2005-09-26 21:57:04 +0000114///
Evan Chenga9c20912006-01-21 02:32:06 +0000115void ScheduleDAG::EmitNode(NodeInfo *NI) {
Jim Laskeyb6d4c2c2005-09-30 19:15:27 +0000116 unsigned VRBase = 0; // First virtual register for node
117 SDNode *Node = NI->Node;
Chris Lattner2d973e42005-08-18 20:07:59 +0000118
Jim Laskeyb6d4c2c2005-09-30 19:15:27 +0000119 // If machine instruction
120 if (Node->isTargetOpcode()) {
121 unsigned Opc = Node->getTargetOpcode();
Evan Chenga9c20912006-01-21 02:32:06 +0000122 const TargetInstrDescriptor &II = TII->get(Opc);
Chris Lattner2d973e42005-08-18 20:07:59 +0000123
Jim Laskeyb6d4c2c2005-09-30 19:15:27 +0000124 unsigned NumResults = CountResults(Node);
125 unsigned NodeOperands = CountOperands(Node);
Jim Laskeye6b90fb2005-09-26 21:57:04 +0000126 unsigned NumMIOperands = NodeOperands + NumResults;
Chris Lattnerda8abb02005-09-01 18:44:10 +0000127#ifndef NDEBUG
Chris Lattner14b392a2005-08-24 22:02:41 +0000128 assert((unsigned(II.numOperands) == NumMIOperands || II.numOperands == -1)&&
Chris Lattner2d973e42005-08-18 20:07:59 +0000129 "#operands for dag node doesn't match .td file!");
Chris Lattnerca6aa2f2005-08-19 01:01:34 +0000130#endif
Chris Lattner2d973e42005-08-18 20:07:59 +0000131
132 // Create the new machine instruction.
Chris Lattner14b392a2005-08-24 22:02:41 +0000133 MachineInstr *MI = new MachineInstr(Opc, NumMIOperands, true, true);
Chris Lattner2d973e42005-08-18 20:07:59 +0000134
135 // Add result register values for things that are defined by this
136 // instruction.
Chris Lattnera4176522005-10-30 18:54:27 +0000137
138 // If the node is only used by a CopyToReg and the dest reg is a vreg, use
139 // the CopyToReg'd destination register instead of creating a new vreg.
140 if (NumResults == 1) {
141 for (SDNode::use_iterator UI = Node->use_begin(), E = Node->use_end();
142 UI != E; ++UI) {
143 SDNode *Use = *UI;
144 if (Use->getOpcode() == ISD::CopyToReg &&
145 Use->getOperand(2).Val == Node) {
146 unsigned Reg = cast<RegisterSDNode>(Use->getOperand(1))->getReg();
147 if (MRegisterInfo::isVirtualRegister(Reg)) {
148 VRBase = Reg;
149 MI->addRegOperand(Reg, MachineOperand::Def);
150 break;
151 }
152 }
153 }
154 }
155
156 // Otherwise, create new virtual registers.
157 if (NumResults && VRBase == 0)
Evan Cheng4ef10862006-01-23 07:01:07 +0000158 VRBase = CreateVirtualRegisters(MI, NumResults, RegMap, II);
Jim Laskeyb6d4c2c2005-09-30 19:15:27 +0000159
160 // Emit all of the actual operands of this instruction, adding them to the
161 // instruction as appropriate.
162 for (unsigned i = 0; i != NodeOperands; ++i) {
163 if (Node->getOperand(i).isTargetOpcode()) {
164 // Note that this case is redundant with the final else block, but we
165 // include it because it is the most common and it makes the logic
166 // simpler here.
167 assert(Node->getOperand(i).getValueType() != MVT::Other &&
168 Node->getOperand(i).getValueType() != MVT::Flag &&
169 "Chain and flag operands should occur at end of operand list!");
Chris Lattner505277a2005-10-01 07:45:09 +0000170
171 // Get/emit the operand.
172 unsigned VReg = getVR(Node->getOperand(i));
173 MI->addRegOperand(VReg, MachineOperand::Use);
Jim Laskeyb6d4c2c2005-09-30 19:15:27 +0000174
Chris Lattner505277a2005-10-01 07:45:09 +0000175 // Verify that it is right.
176 assert(MRegisterInfo::isVirtualRegister(VReg) && "Not a vreg?");
177 assert(II.OpInfo[i+NumResults].RegClass &&
178 "Don't have operand info for this instruction!");
179 assert(RegMap->getRegClass(VReg) == II.OpInfo[i+NumResults].RegClass &&
180 "Register class of operand and regclass of use don't agree!");
Jim Laskeyb6d4c2c2005-09-30 19:15:27 +0000181 } else if (ConstantSDNode *C =
182 dyn_cast<ConstantSDNode>(Node->getOperand(i))) {
183 MI->addZeroExtImm64Operand(C->getValue());
184 } else if (RegisterSDNode*R =
185 dyn_cast<RegisterSDNode>(Node->getOperand(i))) {
186 MI->addRegOperand(R->getReg(), MachineOperand::Use);
187 } else if (GlobalAddressSDNode *TGA =
188 dyn_cast<GlobalAddressSDNode>(Node->getOperand(i))) {
Evan Cheng61ca74b2005-11-30 02:04:11 +0000189 MI->addGlobalAddressOperand(TGA->getGlobal(), false, TGA->getOffset());
Jim Laskeyb6d4c2c2005-09-30 19:15:27 +0000190 } else if (BasicBlockSDNode *BB =
191 dyn_cast<BasicBlockSDNode>(Node->getOperand(i))) {
192 MI->addMachineBasicBlockOperand(BB->getBasicBlock());
193 } else if (FrameIndexSDNode *FI =
194 dyn_cast<FrameIndexSDNode>(Node->getOperand(i))) {
195 MI->addFrameIndexOperand(FI->getIndex());
196 } else if (ConstantPoolSDNode *CP =
197 dyn_cast<ConstantPoolSDNode>(Node->getOperand(i))) {
Chris Lattner948d9662006-02-09 02:23:13 +0000198 unsigned Align = CP->getAlignment();
199 // MachineConstantPool wants an explicit alignment.
200 if (Align == 0) {
201 if (CP->get()->getType() == Type::DoubleTy)
202 Align = 3; // always 8-byte align doubles.
203 else
204 Align = TM.getTargetData()
205 .getTypeAlignmentShift(CP->get()->getType());
206 }
207
208 unsigned Idx = ConstPool->getConstantPoolIndex(CP->get(), Align);
Jim Laskeyb6d4c2c2005-09-30 19:15:27 +0000209 MI->addConstantPoolIndexOperand(Idx);
210 } else if (ExternalSymbolSDNode *ES =
211 dyn_cast<ExternalSymbolSDNode>(Node->getOperand(i))) {
212 MI->addExternalSymbolOperand(ES->getSymbol(), false);
213 } else {
214 assert(Node->getOperand(i).getValueType() != MVT::Other &&
215 Node->getOperand(i).getValueType() != MVT::Flag &&
216 "Chain and flag operands should occur at end of operand list!");
Chris Lattner505277a2005-10-01 07:45:09 +0000217 unsigned VReg = getVR(Node->getOperand(i));
218 MI->addRegOperand(VReg, MachineOperand::Use);
219
220 // Verify that it is right.
221 assert(MRegisterInfo::isVirtualRegister(VReg) && "Not a vreg?");
222 assert(II.OpInfo[i+NumResults].RegClass &&
223 "Don't have operand info for this instruction!");
224 assert(RegMap->getRegClass(VReg) == II.OpInfo[i+NumResults].RegClass &&
225 "Register class of operand and regclass of use don't agree!");
Jim Laskeyb6d4c2c2005-09-30 19:15:27 +0000226 }
227 }
228
229 // Now that we have emitted all operands, emit this instruction itself.
230 if ((II.Flags & M_USES_CUSTOM_DAG_SCHED_INSERTION) == 0) {
231 BB->insert(BB->end(), MI);
232 } else {
233 // Insert this instruction into the end of the basic block, potentially
234 // taking some custom action.
235 BB = DAG.getTargetLoweringInfo().InsertAtEndOfBasicBlock(MI, BB);
236 }
237 } else {
238 switch (Node->getOpcode()) {
239 default:
240 Node->dump();
241 assert(0 && "This target-independent node should have been selected!");
242 case ISD::EntryToken: // fall thru
243 case ISD::TokenFactor:
244 break;
245 case ISD::CopyToReg: {
Chris Lattnera4176522005-10-30 18:54:27 +0000246 unsigned InReg = getVR(Node->getOperand(2));
247 unsigned DestReg = cast<RegisterSDNode>(Node->getOperand(1))->getReg();
248 if (InReg != DestReg) // Coallesced away the copy?
Evan Chenga9c20912006-01-21 02:32:06 +0000249 MRI->copyRegToReg(*BB, BB->end(), DestReg, InReg,
250 RegMap->getRegClass(InReg));
Jim Laskeyb6d4c2c2005-09-30 19:15:27 +0000251 break;
252 }
253 case ISD::CopyFromReg: {
254 unsigned SrcReg = cast<RegisterSDNode>(Node->getOperand(1))->getReg();
Chris Lattner089c25c2005-10-09 05:58:56 +0000255 if (MRegisterInfo::isVirtualRegister(SrcReg)) {
256 VRBase = SrcReg; // Just use the input register directly!
257 break;
258 }
259
Chris Lattnera4176522005-10-30 18:54:27 +0000260 // If the node is only used by a CopyToReg and the dest reg is a vreg, use
261 // the CopyToReg'd destination register instead of creating a new vreg.
262 for (SDNode::use_iterator UI = Node->use_begin(), E = Node->use_end();
263 UI != E; ++UI) {
264 SDNode *Use = *UI;
265 if (Use->getOpcode() == ISD::CopyToReg &&
266 Use->getOperand(2).Val == Node) {
267 unsigned DestReg = cast<RegisterSDNode>(Use->getOperand(1))->getReg();
268 if (MRegisterInfo::isVirtualRegister(DestReg)) {
269 VRBase = DestReg;
270 break;
271 }
272 }
273 }
274
Jim Laskeyb6d4c2c2005-09-30 19:15:27 +0000275 // Figure out the register class to create for the destreg.
276 const TargetRegisterClass *TRC = 0;
Chris Lattnera4176522005-10-30 18:54:27 +0000277 if (VRBase) {
278 TRC = RegMap->getRegClass(VRBase);
279 } else {
Chris Lattner089c25c2005-10-09 05:58:56 +0000280
Chris Lattnera4176522005-10-30 18:54:27 +0000281 // Pick the register class of the right type that contains this physreg.
Evan Chenga9c20912006-01-21 02:32:06 +0000282 for (MRegisterInfo::regclass_iterator I = MRI->regclass_begin(),
283 E = MRI->regclass_end(); I != E; ++I)
Nate Begeman6510b222005-12-01 04:51:06 +0000284 if ((*I)->hasType(Node->getValueType(0)) &&
Chris Lattnera4176522005-10-30 18:54:27 +0000285 (*I)->contains(SrcReg)) {
286 TRC = *I;
287 break;
288 }
289 assert(TRC && "Couldn't find register class for reg copy!");
Jim Laskeyb6d4c2c2005-09-30 19:15:27 +0000290
Chris Lattnera4176522005-10-30 18:54:27 +0000291 // Create the reg, emit the copy.
292 VRBase = RegMap->createVirtualRegister(TRC);
293 }
Evan Chenga9c20912006-01-21 02:32:06 +0000294 MRI->copyRegToReg(*BB, BB->end(), VRBase, SrcReg, TRC);
Jim Laskeyb6d4c2c2005-09-30 19:15:27 +0000295 break;
296 }
Chris Lattneracc43bf2006-01-26 23:28:04 +0000297 case ISD::INLINEASM: {
298 unsigned NumOps = Node->getNumOperands();
299 if (Node->getOperand(NumOps-1).getValueType() == MVT::Flag)
300 --NumOps; // Ignore the flag operand.
301
302 // Create the inline asm machine instruction.
303 MachineInstr *MI =
304 new MachineInstr(BB, TargetInstrInfo::INLINEASM, (NumOps-2)/2+1);
305
306 // Add the asm string as an external symbol operand.
307 const char *AsmStr =
308 cast<ExternalSymbolSDNode>(Node->getOperand(1))->getSymbol();
309 MI->addExternalSymbolOperand(AsmStr, false);
310
311 // Add all of the operand registers to the instruction.
312 for (unsigned i = 2; i != NumOps; i += 2) {
Chris Lattner6656dd12006-01-31 02:03:41 +0000313 unsigned Flags =cast<ConstantSDNode>(Node->getOperand(i+1))->getValue();
Chris Lattneracc43bf2006-01-26 23:28:04 +0000314 switch (Flags) {
315 default: assert(0 && "Bad flags!");
Chris Lattnerdc19b702006-02-04 02:26:14 +0000316 case 1: { // Use of register.
317 unsigned Reg = cast<RegisterSDNode>(Node->getOperand(i))->getReg();
318 MI->addMachineRegOperand(Reg, MachineOperand::Use);
319 break;
Chris Lattneracc43bf2006-01-26 23:28:04 +0000320 }
Chris Lattnerdc19b702006-02-04 02:26:14 +0000321 case 2: { // Def of register.
322 unsigned Reg = cast<RegisterSDNode>(Node->getOperand(i))->getReg();
323 MI->addMachineRegOperand(Reg, MachineOperand::Def);
324 break;
325 }
326 case 3: { // Immediate.
327 uint64_t Val = cast<ConstantSDNode>(Node->getOperand(i))->getValue();
328 MI->addZeroExtImm64Operand(Val);
329 break;
330 }
331 }
Chris Lattneracc43bf2006-01-26 23:28:04 +0000332 }
333 break;
334 }
Jim Laskeyb6d4c2c2005-09-30 19:15:27 +0000335 }
336 }
337
338 assert(NI->VRBase == 0 && "Node emitted out of order - early");
339 NI->VRBase = VRBase;
340}
341
Evan Cheng4ef10862006-01-23 07:01:07 +0000342/// EmitAll - Emit all nodes in schedule sorted order.
343///
344void ScheduleDAG::EmitAll() {
345 // For each node in the ordering
346 for (unsigned i = 0, N = Ordering.size(); i < N; i++) {
347 // Get the scheduling info
348 NodeInfo *NI = Ordering[i];
349 if (NI->isInGroup()) {
350 NodeGroupIterator NGI(Ordering[i]);
351 while (NodeInfo *NI = NGI.next()) EmitNode(NI);
352 } else {
353 EmitNode(NI);
354 }
355 }
356}
357
358/// isFlagDefiner - Returns true if the node defines a flag result.
359static bool isFlagDefiner(SDNode *A) {
360 unsigned N = A->getNumValues();
361 return N && A->getValueType(N - 1) == MVT::Flag;
362}
363
364/// isFlagUser - Returns true if the node uses a flag result.
365///
366static bool isFlagUser(SDNode *A) {
367 unsigned N = A->getNumOperands();
368 return N && A->getOperand(N - 1).getValueType() == MVT::Flag;
369}
370
371/// printNI - Print node info.
372///
373void ScheduleDAG::printNI(std::ostream &O, NodeInfo *NI) const {
374#ifndef NDEBUG
375 SDNode *Node = NI->Node;
376 O << " "
377 << std::hex << Node << std::dec
378 << ", Lat=" << NI->Latency
379 << ", Slot=" << NI->Slot
380 << ", ARITY=(" << Node->getNumOperands() << ","
381 << Node->getNumValues() << ")"
382 << " " << Node->getOperationName(&DAG);
383 if (isFlagDefiner(Node)) O << "<#";
384 if (isFlagUser(Node)) O << ">#";
385#endif
386}
387
388/// printChanges - Hilight changes in order caused by scheduling.
389///
390void ScheduleDAG::printChanges(unsigned Index) const {
391#ifndef NDEBUG
392 // Get the ordered node count
393 unsigned N = Ordering.size();
394 // Determine if any changes
395 unsigned i = 0;
396 for (; i < N; i++) {
397 NodeInfo *NI = Ordering[i];
398 if (NI->Preorder != i) break;
399 }
400
401 if (i < N) {
402 std::cerr << Index << ". New Ordering\n";
403
404 for (i = 0; i < N; i++) {
405 NodeInfo *NI = Ordering[i];
406 std::cerr << " " << NI->Preorder << ". ";
407 printNI(std::cerr, NI);
408 std::cerr << "\n";
409 if (NI->isGroupDominator()) {
410 NodeGroup *Group = NI->Group;
411 for (NIIterator NII = Group->group_begin(), E = Group->group_end();
412 NII != E; NII++) {
413 std::cerr << " ";
414 printNI(std::cerr, *NII);
415 std::cerr << "\n";
416 }
417 }
418 }
419 } else {
420 std::cerr << Index << ". No Changes\n";
421 }
422#endif
423}
424
425/// print - Print ordering to specified output stream.
426///
427void ScheduleDAG::print(std::ostream &O) const {
428#ifndef NDEBUG
429 using namespace std;
430 O << "Ordering\n";
431 for (unsigned i = 0, N = Ordering.size(); i < N; i++) {
432 NodeInfo *NI = Ordering[i];
433 printNI(O, NI);
434 O << "\n";
435 if (NI->isGroupDominator()) {
436 NodeGroup *Group = NI->Group;
437 for (NIIterator NII = Group->group_begin(), E = Group->group_end();
438 NII != E; NII++) {
439 O << " ";
440 printNI(O, *NII);
441 O << "\n";
442 }
443 }
444 }
445#endif
446}
447
Evan Chenga9c20912006-01-21 02:32:06 +0000448void ScheduleDAG::dump(const char *tag) const {
449 std::cerr << tag; dump();
Jim Laskeyfab66f62005-10-12 18:29:35 +0000450}
451
Evan Chenga9c20912006-01-21 02:32:06 +0000452void ScheduleDAG::dump() const {
Jim Laskeye6b90fb2005-09-26 21:57:04 +0000453 print(std::cerr);
454}
Jim Laskeye6b90fb2005-09-26 21:57:04 +0000455
Evan Chenga9c20912006-01-21 02:32:06 +0000456/// Run - perform scheduling.
457///
458MachineBasicBlock *ScheduleDAG::Run() {
459 TII = TM.getInstrInfo();
460 MRI = TM.getRegisterInfo();
461 RegMap = BB->getParent()->getSSARegMap();
462 ConstPool = BB->getParent()->getConstantPool();
Evan Cheng4ef10862006-01-23 07:01:07 +0000463
464 // Number the nodes
465 NodeCount = std::distance(DAG.allnodes_begin(), DAG.allnodes_end());
466 // Set up minimum info for scheduling
467 PrepareNodeInfo();
468 // Construct node groups for flagged nodes
469 IdentifyGroups();
470
Evan Chenga9c20912006-01-21 02:32:06 +0000471 Schedule();
472 return BB;
Chris Lattnerd32b2362005-08-18 18:45:24 +0000473}
Evan Cheng4ef10862006-01-23 07:01:07 +0000474
475
476/// CountInternalUses - Returns the number of edges between the two nodes.
477///
478static unsigned CountInternalUses(NodeInfo *D, NodeInfo *U) {
479 unsigned N = 0;
480 for (unsigned M = U->Node->getNumOperands(); 0 < M--;) {
481 SDOperand Op = U->Node->getOperand(M);
482 if (Op.Val == D->Node) N++;
483 }
484
485 return N;
486}
487
488//===----------------------------------------------------------------------===//
489/// Add - Adds a definer and user pair to a node group.
490///
Evan Chengcccf1232006-02-04 06:49:00 +0000491void ScheduleDAG::AddToGroup(NodeInfo *D, NodeInfo *U) {
Evan Cheng4ef10862006-01-23 07:01:07 +0000492 // Get current groups
493 NodeGroup *DGroup = D->Group;
494 NodeGroup *UGroup = U->Group;
495 // If both are members of groups
496 if (DGroup && UGroup) {
497 // There may have been another edge connecting
498 if (DGroup == UGroup) return;
499 // Add the pending users count
500 DGroup->addPending(UGroup->getPending());
501 // For each member of the users group
502 NodeGroupIterator UNGI(U);
503 while (NodeInfo *UNI = UNGI.next() ) {
504 // Change the group
505 UNI->Group = DGroup;
506 // For each member of the definers group
507 NodeGroupIterator DNGI(D);
508 while (NodeInfo *DNI = DNGI.next() ) {
509 // Remove internal edges
510 DGroup->addPending(-CountInternalUses(DNI, UNI));
511 }
512 }
513 // Merge the two lists
514 DGroup->group_insert(DGroup->group_end(),
515 UGroup->group_begin(), UGroup->group_end());
516 } else if (DGroup) {
517 // Make user member of definers group
518 U->Group = DGroup;
519 // Add users uses to definers group pending
520 DGroup->addPending(U->Node->use_size());
521 // For each member of the definers group
522 NodeGroupIterator DNGI(D);
523 while (NodeInfo *DNI = DNGI.next() ) {
524 // Remove internal edges
525 DGroup->addPending(-CountInternalUses(DNI, U));
526 }
527 DGroup->group_push_back(U);
528 } else if (UGroup) {
529 // Make definer member of users group
530 D->Group = UGroup;
531 // Add definers uses to users group pending
532 UGroup->addPending(D->Node->use_size());
533 // For each member of the users group
534 NodeGroupIterator UNGI(U);
535 while (NodeInfo *UNI = UNGI.next() ) {
536 // Remove internal edges
537 UGroup->addPending(-CountInternalUses(D, UNI));
538 }
539 UGroup->group_insert(UGroup->group_begin(), D);
540 } else {
541 D->Group = U->Group = DGroup = new NodeGroup();
542 DGroup->addPending(D->Node->use_size() + U->Node->use_size() -
543 CountInternalUses(D, U));
544 DGroup->group_push_back(D);
545 DGroup->group_push_back(U);
Evan Chengcccf1232006-02-04 06:49:00 +0000546
547 if (HeadNG == NULL)
548 HeadNG = DGroup;
549 if (TailNG != NULL)
550 TailNG->Next = DGroup;
551 TailNG = DGroup;
Evan Cheng4ef10862006-01-23 07:01:07 +0000552 }
553}