blob: 4a9b9c7f04e2264e3298f32ed7e5e4bd30e1c333 [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();
123 if (TII->isTwoAddrInstr(Opc)) {
124 SU->isTwoAddress = true;
125 SDNode *OpN = MainNode->getOperand(0).Val;
126 SUnit *OpSU = SUnitMap[OpN];
127 if (OpSU)
128 OpSU->isDefNUseOperand = true;
129 }
130 }
131
132 // Find all predecessors and successors of the group.
133 // Temporarily add N to make code simpler.
134 SU->FlaggedNodes.push_back(MainNode);
135
136 for (unsigned n = 0, e = SU->FlaggedNodes.size(); n != e; ++n) {
137 SDNode *N = SU->FlaggedNodes[n];
138
139 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
140 SDNode *OpN = N->getOperand(i).Val;
141 if (isPassiveNode(OpN)) continue; // Not scheduled.
142 SUnit *OpSU = SUnitMap[OpN];
143 assert(OpSU && "Node has no SUnit!");
144 if (OpSU == SU) continue; // In the same group.
145
146 MVT::ValueType OpVT = N->getOperand(i).getValueType();
147 assert(OpVT != MVT::Flag && "Flagged nodes should be in same sunit!");
148 bool isChain = OpVT == MVT::Other;
149
150 if (SU->Preds.insert(std::make_pair(OpSU, isChain)).second) {
151 if (!isChain) {
152 SU->NumPreds++;
153 SU->NumPredsLeft++;
154 } else {
155 SU->NumChainPredsLeft++;
156 }
157 }
158 if (OpSU->Succs.insert(std::make_pair(SU, isChain)).second) {
159 if (!isChain) {
160 OpSU->NumSuccs++;
161 OpSU->NumSuccsLeft++;
162 } else {
163 OpSU->NumChainSuccsLeft++;
164 }
165 }
166 }
167 }
168
169 // Remove MainNode from FlaggedNodes again.
170 SU->FlaggedNodes.pop_back();
171 }
172
173 return;
174}
175
176static void CalculateDepths(SUnit *SU, unsigned Depth) {
177 if (Depth > SU->Depth) SU->Depth = Depth;
178 for (std::set<std::pair<SUnit*, bool> >::iterator I = SU->Succs.begin(),
179 E = SU->Succs.end(); I != E; ++I)
180 CalculateDepths(I->first, Depth+1);
181}
182
183void ScheduleDAG::CalculateDepths() {
184 SUnit *Entry = SUnitMap[DAG.getEntryNode().Val];
185 ::CalculateDepths(Entry, 0U);
186 for (unsigned i = 0, e = SUnits.size(); i != e; ++i)
187 if (SUnits[i].Preds.size() == 0 && &SUnits[i] != Entry) {
188 ::CalculateDepths(&SUnits[i], 0U);
189 }
190}
191
192static void CalculateHeights(SUnit *SU, unsigned Height) {
193 if (Height > SU->Height) 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}
198void ScheduleDAG::CalculateHeights() {
199 SUnit *Root = SUnitMap[DAG.getRoot().Val];
200 ::CalculateHeights(Root, 0U);
201}
202
Jim Laskeye6b90fb2005-09-26 21:57:04 +0000203/// CountResults - The results of target nodes have register or immediate
204/// operands first, then an optional chain, and optional flag operands (which do
205/// not go into the machine instrs.)
Evan Chenga9c20912006-01-21 02:32:06 +0000206static unsigned CountResults(SDNode *Node) {
Jim Laskeyb6d4c2c2005-09-30 19:15:27 +0000207 unsigned N = Node->getNumValues();
208 while (N && Node->getValueType(N - 1) == MVT::Flag)
Jim Laskeye6b90fb2005-09-26 21:57:04 +0000209 --N;
Jim Laskeyb6d4c2c2005-09-30 19:15:27 +0000210 if (N && Node->getValueType(N - 1) == MVT::Other)
Jim Laskeye6b90fb2005-09-26 21:57:04 +0000211 --N; // Skip over chain result.
212 return N;
213}
214
215/// CountOperands The inputs to target nodes have any actual inputs first,
216/// followed by an optional chain operand, then flag operands. Compute the
217/// number of actual operands that will go into the machine instr.
Evan Chenga9c20912006-01-21 02:32:06 +0000218static unsigned CountOperands(SDNode *Node) {
Jim Laskeyb6d4c2c2005-09-30 19:15:27 +0000219 unsigned N = Node->getNumOperands();
220 while (N && Node->getOperand(N - 1).getValueType() == MVT::Flag)
Jim Laskeye6b90fb2005-09-26 21:57:04 +0000221 --N;
Jim Laskeyb6d4c2c2005-09-30 19:15:27 +0000222 if (N && Node->getOperand(N - 1).getValueType() == MVT::Other)
Jim Laskeye6b90fb2005-09-26 21:57:04 +0000223 --N; // Ignore chain if it exists.
224 return N;
225}
226
Evan Cheng4ef10862006-01-23 07:01:07 +0000227static unsigned CreateVirtualRegisters(MachineInstr *MI,
228 unsigned NumResults,
229 SSARegMap *RegMap,
230 const TargetInstrDescriptor &II) {
Jim Laskeye6b90fb2005-09-26 21:57:04 +0000231 // Create the result registers for this node and add the result regs to
232 // the machine instruction.
233 const TargetOperandInfo *OpInfo = II.OpInfo;
234 unsigned ResultReg = RegMap->createVirtualRegister(OpInfo[0].RegClass);
235 MI->addRegOperand(ResultReg, MachineOperand::Def);
236 for (unsigned i = 1; i != NumResults; ++i) {
237 assert(OpInfo[i].RegClass && "Isn't a register operand!");
Chris Lattner505277a2005-10-01 07:45:09 +0000238 MI->addRegOperand(RegMap->createVirtualRegister(OpInfo[i].RegClass),
Jim Laskeye6b90fb2005-09-26 21:57:04 +0000239 MachineOperand::Def);
240 }
241 return ResultReg;
242}
243
Chris Lattnerdf375062006-03-10 07:25:12 +0000244/// getVR - Return the virtual register corresponding to the specified result
245/// of the specified node.
246static unsigned getVR(SDOperand Op, std::map<SDNode*, unsigned> &VRBaseMap) {
247 std::map<SDNode*, unsigned>::iterator I = VRBaseMap.find(Op.Val);
248 assert(I != VRBaseMap.end() && "Node emitted out of order - late");
249 return I->second + Op.ResNo;
250}
251
252
Chris Lattnered18b682006-02-24 18:54:03 +0000253/// AddOperand - Add the specified operand to the specified machine instr. II
254/// specifies the instruction information for the node, and IIOpNum is the
255/// operand number (in the II) that we are adding. IIOpNum and II are used for
256/// assertions only.
257void ScheduleDAG::AddOperand(MachineInstr *MI, SDOperand Op,
258 unsigned IIOpNum,
Chris Lattnerdf375062006-03-10 07:25:12 +0000259 const TargetInstrDescriptor *II,
260 std::map<SDNode*, unsigned> &VRBaseMap) {
Chris Lattnered18b682006-02-24 18:54:03 +0000261 if (Op.isTargetOpcode()) {
262 // Note that this case is redundant with the final else block, but we
263 // include it because it is the most common and it makes the logic
264 // simpler here.
265 assert(Op.getValueType() != MVT::Other &&
266 Op.getValueType() != MVT::Flag &&
267 "Chain and flag operands should occur at end of operand list!");
268
269 // Get/emit the operand.
Chris Lattnerdf375062006-03-10 07:25:12 +0000270 unsigned VReg = getVR(Op, VRBaseMap);
Chris Lattnered18b682006-02-24 18:54:03 +0000271 MI->addRegOperand(VReg, MachineOperand::Use);
272
273 // Verify that it is right.
274 assert(MRegisterInfo::isVirtualRegister(VReg) && "Not a vreg?");
275 if (II) {
276 assert(II->OpInfo[IIOpNum].RegClass &&
277 "Don't have operand info for this instruction!");
278 assert(RegMap->getRegClass(VReg) == II->OpInfo[IIOpNum].RegClass &&
279 "Register class of operand and regclass of use don't agree!");
280 }
281 } else if (ConstantSDNode *C =
282 dyn_cast<ConstantSDNode>(Op)) {
Chris Lattner2d90ac72006-05-04 18:05:43 +0000283 MI->addImmOperand(C->getValue());
Chris Lattnered18b682006-02-24 18:54:03 +0000284 } else if (RegisterSDNode*R =
285 dyn_cast<RegisterSDNode>(Op)) {
286 MI->addRegOperand(R->getReg(), MachineOperand::Use);
287 } else if (GlobalAddressSDNode *TGA =
288 dyn_cast<GlobalAddressSDNode>(Op)) {
Chris Lattnerea50fab2006-05-04 01:15:02 +0000289 MI->addGlobalAddressOperand(TGA->getGlobal(), TGA->getOffset());
Chris Lattnered18b682006-02-24 18:54:03 +0000290 } else if (BasicBlockSDNode *BB =
291 dyn_cast<BasicBlockSDNode>(Op)) {
292 MI->addMachineBasicBlockOperand(BB->getBasicBlock());
293 } else if (FrameIndexSDNode *FI =
294 dyn_cast<FrameIndexSDNode>(Op)) {
295 MI->addFrameIndexOperand(FI->getIndex());
Nate Begeman37efe672006-04-22 18:53:45 +0000296 } else if (JumpTableSDNode *JT =
297 dyn_cast<JumpTableSDNode>(Op)) {
298 MI->addJumpTableIndexOperand(JT->getIndex());
Chris Lattnered18b682006-02-24 18:54:03 +0000299 } else if (ConstantPoolSDNode *CP =
300 dyn_cast<ConstantPoolSDNode>(Op)) {
Evan Cheng404cb4f2006-02-25 09:54:52 +0000301 int Offset = CP->getOffset();
Chris Lattnered18b682006-02-24 18:54:03 +0000302 unsigned Align = CP->getAlignment();
303 // MachineConstantPool wants an explicit alignment.
304 if (Align == 0) {
305 if (CP->get()->getType() == Type::DoubleTy)
306 Align = 3; // always 8-byte align doubles.
Chris Lattner54a30b92006-03-20 01:51:46 +0000307 else {
Chris Lattnered18b682006-02-24 18:54:03 +0000308 Align = TM.getTargetData()
Owen Andersona69571c2006-05-03 01:29:57 +0000309 ->getTypeAlignmentShift(CP->get()->getType());
Chris Lattner54a30b92006-03-20 01:51:46 +0000310 if (Align == 0) {
311 // Alignment of packed types. FIXME!
Owen Andersona69571c2006-05-03 01:29:57 +0000312 Align = TM.getTargetData()->getTypeSize(CP->get()->getType());
Chris Lattner54a30b92006-03-20 01:51:46 +0000313 Align = Log2_64(Align);
314 }
315 }
Chris Lattnered18b682006-02-24 18:54:03 +0000316 }
317
318 unsigned Idx = ConstPool->getConstantPoolIndex(CP->get(), Align);
Evan Cheng404cb4f2006-02-25 09:54:52 +0000319 MI->addConstantPoolIndexOperand(Idx, Offset);
Chris Lattnered18b682006-02-24 18:54:03 +0000320 } else if (ExternalSymbolSDNode *ES =
321 dyn_cast<ExternalSymbolSDNode>(Op)) {
Chris Lattnerea50fab2006-05-04 01:15:02 +0000322 MI->addExternalSymbolOperand(ES->getSymbol());
Chris Lattnered18b682006-02-24 18:54:03 +0000323 } else {
324 assert(Op.getValueType() != MVT::Other &&
325 Op.getValueType() != MVT::Flag &&
326 "Chain and flag operands should occur at end of operand list!");
Chris Lattnerdf375062006-03-10 07:25:12 +0000327 unsigned VReg = getVR(Op, VRBaseMap);
Chris Lattnered18b682006-02-24 18:54:03 +0000328 MI->addRegOperand(VReg, MachineOperand::Use);
329
330 // Verify that it is right.
331 assert(MRegisterInfo::isVirtualRegister(VReg) && "Not a vreg?");
332 if (II) {
333 assert(II->OpInfo[IIOpNum].RegClass &&
334 "Don't have operand info for this instruction!");
335 assert(RegMap->getRegClass(VReg) == II->OpInfo[IIOpNum].RegClass &&
336 "Register class of operand and regclass of use don't agree!");
337 }
338 }
339
340}
341
342
Jim Laskeyb6d4c2c2005-09-30 19:15:27 +0000343/// EmitNode - Generate machine code for an node and needed dependencies.
Jim Laskeye6b90fb2005-09-26 21:57:04 +0000344///
Chris Lattner8c7ef052006-03-10 07:28:36 +0000345void ScheduleDAG::EmitNode(SDNode *Node,
Chris Lattnerdf375062006-03-10 07:25:12 +0000346 std::map<SDNode*, unsigned> &VRBaseMap) {
Jim Laskeyb6d4c2c2005-09-30 19:15:27 +0000347 unsigned VRBase = 0; // First virtual register for node
Chris Lattner2d973e42005-08-18 20:07:59 +0000348
Jim Laskeyb6d4c2c2005-09-30 19:15:27 +0000349 // If machine instruction
350 if (Node->isTargetOpcode()) {
351 unsigned Opc = Node->getTargetOpcode();
Evan Chenga9c20912006-01-21 02:32:06 +0000352 const TargetInstrDescriptor &II = TII->get(Opc);
Chris Lattner2d973e42005-08-18 20:07:59 +0000353
Jim Laskeyb6d4c2c2005-09-30 19:15:27 +0000354 unsigned NumResults = CountResults(Node);
355 unsigned NodeOperands = CountOperands(Node);
Jim Laskeye6b90fb2005-09-26 21:57:04 +0000356 unsigned NumMIOperands = NodeOperands + NumResults;
Chris Lattnerda8abb02005-09-01 18:44:10 +0000357#ifndef NDEBUG
Chris Lattner14b392a2005-08-24 22:02:41 +0000358 assert((unsigned(II.numOperands) == NumMIOperands || II.numOperands == -1)&&
Chris Lattner2d973e42005-08-18 20:07:59 +0000359 "#operands for dag node doesn't match .td file!");
Chris Lattnerca6aa2f2005-08-19 01:01:34 +0000360#endif
Chris Lattner2d973e42005-08-18 20:07:59 +0000361
362 // Create the new machine instruction.
Chris Lattner8b915b42006-05-04 18:16:01 +0000363 MachineInstr *MI = new MachineInstr(Opc, NumMIOperands);
Chris Lattner2d973e42005-08-18 20:07:59 +0000364
365 // Add result register values for things that are defined by this
366 // instruction.
Chris Lattnera4176522005-10-30 18:54:27 +0000367
368 // If the node is only used by a CopyToReg and the dest reg is a vreg, use
369 // the CopyToReg'd destination register instead of creating a new vreg.
370 if (NumResults == 1) {
371 for (SDNode::use_iterator UI = Node->use_begin(), E = Node->use_end();
372 UI != E; ++UI) {
373 SDNode *Use = *UI;
374 if (Use->getOpcode() == ISD::CopyToReg &&
375 Use->getOperand(2).Val == Node) {
376 unsigned Reg = cast<RegisterSDNode>(Use->getOperand(1))->getReg();
377 if (MRegisterInfo::isVirtualRegister(Reg)) {
378 VRBase = Reg;
379 MI->addRegOperand(Reg, MachineOperand::Def);
380 break;
381 }
382 }
383 }
384 }
385
386 // Otherwise, create new virtual registers.
387 if (NumResults && VRBase == 0)
Evan Cheng4ef10862006-01-23 07:01:07 +0000388 VRBase = CreateVirtualRegisters(MI, NumResults, RegMap, II);
Jim Laskeyb6d4c2c2005-09-30 19:15:27 +0000389
390 // Emit all of the actual operands of this instruction, adding them to the
391 // instruction as appropriate.
Chris Lattnered18b682006-02-24 18:54:03 +0000392 for (unsigned i = 0; i != NodeOperands; ++i)
Chris Lattnerdf375062006-03-10 07:25:12 +0000393 AddOperand(MI, Node->getOperand(i), i+NumResults, &II, VRBaseMap);
Jim Laskeyb6d4c2c2005-09-30 19:15:27 +0000394
395 // Now that we have emitted all operands, emit this instruction itself.
396 if ((II.Flags & M_USES_CUSTOM_DAG_SCHED_INSERTION) == 0) {
397 BB->insert(BB->end(), MI);
398 } else {
399 // Insert this instruction into the end of the basic block, potentially
400 // taking some custom action.
401 BB = DAG.getTargetLoweringInfo().InsertAtEndOfBasicBlock(MI, BB);
402 }
403 } else {
404 switch (Node->getOpcode()) {
405 default:
406 Node->dump();
407 assert(0 && "This target-independent node should have been selected!");
408 case ISD::EntryToken: // fall thru
409 case ISD::TokenFactor:
410 break;
411 case ISD::CopyToReg: {
Chris Lattnerdf375062006-03-10 07:25:12 +0000412 unsigned InReg = getVR(Node->getOperand(2), VRBaseMap);
Chris Lattnera4176522005-10-30 18:54:27 +0000413 unsigned DestReg = cast<RegisterSDNode>(Node->getOperand(1))->getReg();
Chris Lattner45053fc2006-03-24 07:15:07 +0000414 if (InReg != DestReg) // Coalesced away the copy?
Evan Chenga9c20912006-01-21 02:32:06 +0000415 MRI->copyRegToReg(*BB, BB->end(), DestReg, InReg,
416 RegMap->getRegClass(InReg));
Jim Laskeyb6d4c2c2005-09-30 19:15:27 +0000417 break;
418 }
419 case ISD::CopyFromReg: {
420 unsigned SrcReg = cast<RegisterSDNode>(Node->getOperand(1))->getReg();
Chris Lattner089c25c2005-10-09 05:58:56 +0000421 if (MRegisterInfo::isVirtualRegister(SrcReg)) {
422 VRBase = SrcReg; // Just use the input register directly!
423 break;
424 }
425
Chris Lattnera4176522005-10-30 18:54:27 +0000426 // If the node is only used by a CopyToReg and the dest reg is a vreg, use
427 // the CopyToReg'd destination register instead of creating a new vreg.
428 for (SDNode::use_iterator UI = Node->use_begin(), E = Node->use_end();
429 UI != E; ++UI) {
430 SDNode *Use = *UI;
431 if (Use->getOpcode() == ISD::CopyToReg &&
432 Use->getOperand(2).Val == Node) {
433 unsigned DestReg = cast<RegisterSDNode>(Use->getOperand(1))->getReg();
434 if (MRegisterInfo::isVirtualRegister(DestReg)) {
435 VRBase = DestReg;
436 break;
437 }
438 }
439 }
440
Jim Laskeyb6d4c2c2005-09-30 19:15:27 +0000441 // Figure out the register class to create for the destreg.
442 const TargetRegisterClass *TRC = 0;
Chris Lattnera4176522005-10-30 18:54:27 +0000443 if (VRBase) {
444 TRC = RegMap->getRegClass(VRBase);
445 } else {
Chris Lattner089c25c2005-10-09 05:58:56 +0000446
Chris Lattnera4176522005-10-30 18:54:27 +0000447 // Pick the register class of the right type that contains this physreg.
Evan Chenga9c20912006-01-21 02:32:06 +0000448 for (MRegisterInfo::regclass_iterator I = MRI->regclass_begin(),
449 E = MRI->regclass_end(); I != E; ++I)
Nate Begeman6510b222005-12-01 04:51:06 +0000450 if ((*I)->hasType(Node->getValueType(0)) &&
Chris Lattnera4176522005-10-30 18:54:27 +0000451 (*I)->contains(SrcReg)) {
452 TRC = *I;
453 break;
454 }
455 assert(TRC && "Couldn't find register class for reg copy!");
Jim Laskeyb6d4c2c2005-09-30 19:15:27 +0000456
Chris Lattnera4176522005-10-30 18:54:27 +0000457 // Create the reg, emit the copy.
458 VRBase = RegMap->createVirtualRegister(TRC);
459 }
Evan Chenga9c20912006-01-21 02:32:06 +0000460 MRI->copyRegToReg(*BB, BB->end(), VRBase, SrcReg, TRC);
Jim Laskeyb6d4c2c2005-09-30 19:15:27 +0000461 break;
462 }
Chris Lattneracc43bf2006-01-26 23:28:04 +0000463 case ISD::INLINEASM: {
464 unsigned NumOps = Node->getNumOperands();
465 if (Node->getOperand(NumOps-1).getValueType() == MVT::Flag)
466 --NumOps; // Ignore the flag operand.
467
468 // Create the inline asm machine instruction.
469 MachineInstr *MI =
470 new MachineInstr(BB, TargetInstrInfo::INLINEASM, (NumOps-2)/2+1);
471
472 // Add the asm string as an external symbol operand.
473 const char *AsmStr =
474 cast<ExternalSymbolSDNode>(Node->getOperand(1))->getSymbol();
Chris Lattnerea50fab2006-05-04 01:15:02 +0000475 MI->addExternalSymbolOperand(AsmStr);
Chris Lattneracc43bf2006-01-26 23:28:04 +0000476
477 // Add all of the operand registers to the instruction.
Chris Lattnerc3a9f8d2006-02-23 19:21:04 +0000478 for (unsigned i = 2; i != NumOps;) {
479 unsigned Flags = cast<ConstantSDNode>(Node->getOperand(i))->getValue();
Chris Lattnerfd6d2822006-02-24 19:18:20 +0000480 unsigned NumVals = Flags >> 3;
Chris Lattnerc3a9f8d2006-02-23 19:21:04 +0000481
Chris Lattner2d90ac72006-05-04 18:05:43 +0000482 MI->addImmOperand(Flags);
Chris Lattnerc3a9f8d2006-02-23 19:21:04 +0000483 ++i; // Skip the ID value.
484
485 switch (Flags & 7) {
Chris Lattneracc43bf2006-01-26 23:28:04 +0000486 default: assert(0 && "Bad flags!");
Chris Lattnerc3a9f8d2006-02-23 19:21:04 +0000487 case 1: // Use of register.
Chris Lattnerfd6d2822006-02-24 19:18:20 +0000488 for (; NumVals; --NumVals, ++i) {
Chris Lattnerc3a9f8d2006-02-23 19:21:04 +0000489 unsigned Reg = cast<RegisterSDNode>(Node->getOperand(i))->getReg();
Chris Lattnerea50fab2006-05-04 01:15:02 +0000490 MI->addRegOperand(Reg, MachineOperand::Use);
Chris Lattnerc3a9f8d2006-02-23 19:21:04 +0000491 }
Chris Lattnerdc19b702006-02-04 02:26:14 +0000492 break;
Chris Lattnerc3a9f8d2006-02-23 19:21:04 +0000493 case 2: // Def of register.
Chris Lattnerfd6d2822006-02-24 19:18:20 +0000494 for (; NumVals; --NumVals, ++i) {
Chris Lattnerc3a9f8d2006-02-23 19:21:04 +0000495 unsigned Reg = cast<RegisterSDNode>(Node->getOperand(i))->getReg();
Chris Lattnerea50fab2006-05-04 01:15:02 +0000496 MI->addRegOperand(Reg, MachineOperand::Def);
Chris Lattnerc3a9f8d2006-02-23 19:21:04 +0000497 }
Chris Lattnerdc19b702006-02-04 02:26:14 +0000498 break;
Chris Lattnerdc19b702006-02-04 02:26:14 +0000499 case 3: { // Immediate.
Chris Lattnerfd6d2822006-02-24 19:18:20 +0000500 assert(NumVals == 1 && "Unknown immediate value!");
Chris Lattnerdc19b702006-02-04 02:26:14 +0000501 uint64_t Val = cast<ConstantSDNode>(Node->getOperand(i))->getValue();
Chris Lattner2d90ac72006-05-04 18:05:43 +0000502 MI->addImmOperand(Val);
Chris Lattnerc3a9f8d2006-02-23 19:21:04 +0000503 ++i;
Chris Lattnerdc19b702006-02-04 02:26:14 +0000504 break;
505 }
Chris Lattnerfd6d2822006-02-24 19:18:20 +0000506 case 4: // Addressing mode.
507 // The addressing mode has been selected, just add all of the
508 // operands to the machine instruction.
509 for (; NumVals; --NumVals, ++i)
Chris Lattnerdf375062006-03-10 07:25:12 +0000510 AddOperand(MI, Node->getOperand(i), 0, 0, VRBaseMap);
Chris Lattnerfd6d2822006-02-24 19:18:20 +0000511 break;
Chris Lattnerdc19b702006-02-04 02:26:14 +0000512 }
Chris Lattneracc43bf2006-01-26 23:28:04 +0000513 }
514 break;
515 }
Jim Laskeyb6d4c2c2005-09-30 19:15:27 +0000516 }
517 }
518
Chris Lattnerdf375062006-03-10 07:25:12 +0000519 assert(!VRBaseMap.count(Node) && "Node emitted out of order - early");
520 VRBaseMap[Node] = VRBase;
Jim Laskeyb6d4c2c2005-09-30 19:15:27 +0000521}
522
Chris Lattnera93dfcd2006-03-05 23:51:47 +0000523void ScheduleDAG::EmitNoop() {
524 TII->insertNoop(*BB, BB->end());
525}
526
Evan Chenge165a782006-05-11 23:55:42 +0000527/// EmitSchedule - Emit the machine code in scheduled order.
528void ScheduleDAG::EmitSchedule() {
529 std::map<SDNode*, unsigned> VRBaseMap;
530 for (unsigned i = 0, e = Sequence.size(); i != e; i++) {
531 if (SUnit *SU = Sequence[i]) {
532 for (unsigned j = 0, ee = SU->FlaggedNodes.size(); j != ee; j++)
533 EmitNode(SU->FlaggedNodes[j], VRBaseMap);
534 EmitNode(SU->Node, VRBaseMap);
535 } else {
536 // Null SUnit* is a noop.
537 EmitNoop();
538 }
539 }
540}
541
542/// dump - dump the schedule.
543void ScheduleDAG::dumpSchedule() const {
544 for (unsigned i = 0, e = Sequence.size(); i != e; i++) {
545 if (SUnit *SU = Sequence[i])
546 SU->dump(&DAG);
547 else
548 std::cerr << "**** NOOP ****\n";
549 }
550}
551
552
Evan Chenga9c20912006-01-21 02:32:06 +0000553/// Run - perform scheduling.
554///
555MachineBasicBlock *ScheduleDAG::Run() {
556 TII = TM.getInstrInfo();
557 MRI = TM.getRegisterInfo();
558 RegMap = BB->getParent()->getSSARegMap();
559 ConstPool = BB->getParent()->getConstantPool();
Evan Cheng4ef10862006-01-23 07:01:07 +0000560
Evan Chenga9c20912006-01-21 02:32:06 +0000561 Schedule();
562 return BB;
Chris Lattnerd32b2362005-08-18 18:45:24 +0000563}
Evan Cheng4ef10862006-01-23 07:01:07 +0000564
Evan Chenge165a782006-05-11 23:55:42 +0000565/// SUnit - Scheduling unit. It's an wrapper around either a single SDNode or
566/// a group of nodes flagged together.
567void SUnit::dump(const SelectionDAG *G) const {
568 std::cerr << "SU(" << NodeNum << "): ";
569 Node->dump(G);
570 std::cerr << "\n";
571 if (FlaggedNodes.size() != 0) {
572 for (unsigned i = 0, e = FlaggedNodes.size(); i != e; i++) {
573 std::cerr << " ";
574 FlaggedNodes[i]->dump(G);
575 std::cerr << "\n";
576 }
577 }
578}
Evan Cheng4ef10862006-01-23 07:01:07 +0000579
Evan Chenge165a782006-05-11 23:55:42 +0000580void SUnit::dumpAll(const SelectionDAG *G) const {
581 dump(G);
582
583 std::cerr << " # preds left : " << NumPredsLeft << "\n";
584 std::cerr << " # succs left : " << NumSuccsLeft << "\n";
585 std::cerr << " # chain preds left : " << NumChainPredsLeft << "\n";
586 std::cerr << " # chain succs left : " << NumChainSuccsLeft << "\n";
587 std::cerr << " Latency : " << Latency << "\n";
588 std::cerr << " Depth : " << Depth << "\n";
589 std::cerr << " Height : " << Height << "\n";
590
591 if (Preds.size() != 0) {
592 std::cerr << " Predecessors:\n";
593 for (std::set<std::pair<SUnit*,bool> >::const_iterator I = Preds.begin(),
594 E = Preds.end(); I != E; ++I) {
595 if (I->second)
596 std::cerr << " ch ";
597 else
598 std::cerr << " val ";
599 I->first->dump(G);
600 }
601 }
602 if (Succs.size() != 0) {
603 std::cerr << " Successors:\n";
604 for (std::set<std::pair<SUnit*, bool> >::const_iterator I = Succs.begin(),
605 E = Succs.end(); I != E; ++I) {
606 if (I->second)
607 std::cerr << " ch ";
608 else
609 std::cerr << " val ";
610 I->first->dump(G);
611 }
612 }
613 std::cerr << "\n";
614}