blob: 97eccb2b95923960d14da374d0374f156c53a846 [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"
Owen Anderson07000c62006-05-12 06:33:49 +000021#include "llvm/Target/TargetData.h"
Chris Lattner2d973e42005-08-18 20:07:59 +000022#include "llvm/Target/TargetMachine.h"
23#include "llvm/Target/TargetInstrInfo.h"
Chris Lattner025c39b2005-08-26 20:54:47 +000024#include "llvm/Target/TargetLowering.h"
Evan Chenge165a782006-05-11 23:55:42 +000025#include "llvm/Support/Debug.h"
Chris Lattner54a30b92006-03-20 01:51:46 +000026#include "llvm/Support/MathExtras.h"
Evan Chenge165a782006-05-11 23:55:42 +000027#include <iostream>
Chris Lattnerd32b2362005-08-18 18:45:24 +000028using namespace llvm;
29
Jim Laskeye6b90fb2005-09-26 21:57:04 +000030
Evan Chenge165a782006-05-11 23:55:42 +000031/// BuildSchedUnits - Build SUnits from the selection dag that we are input.
32/// This SUnit graph is similar to the SelectionDAG, but represents flagged
33/// together nodes with a single SUnit.
34void ScheduleDAG::BuildSchedUnits() {
35 // Reserve entries in the vector for each of the SUnits we are creating. This
36 // ensure that reallocation of the vector won't happen, so SUnit*'s won't get
37 // invalidated.
38 SUnits.reserve(std::distance(DAG.allnodes_begin(), DAG.allnodes_end()));
39
40 const InstrItineraryData &InstrItins = TM.getInstrItineraryData();
41
42 for (SelectionDAG::allnodes_iterator NI = DAG.allnodes_begin(),
43 E = DAG.allnodes_end(); NI != E; ++NI) {
44 if (isPassiveNode(NI)) // Leaf node, e.g. a TargetImmediate.
45 continue;
46
47 // If this node has already been processed, stop now.
48 if (SUnitMap[NI]) continue;
49
50 SUnit *NodeSUnit = NewSUnit(NI);
51
52 // See if anything is flagged to this node, if so, add them to flagged
53 // nodes. Nodes can have at most one flag input and one flag output. Flags
54 // are required the be the last operand and result of a node.
55
56 // Scan up, adding flagged preds to FlaggedNodes.
57 SDNode *N = NI;
58 while (N->getNumOperands() &&
59 N->getOperand(N->getNumOperands()-1).getValueType() == MVT::Flag) {
60 N = N->getOperand(N->getNumOperands()-1).Val;
61 NodeSUnit->FlaggedNodes.push_back(N);
62 SUnitMap[N] = NodeSUnit;
63 }
64
65 // Scan down, adding this node and any flagged succs to FlaggedNodes if they
66 // have a user of the flag operand.
67 N = NI;
68 while (N->getValueType(N->getNumValues()-1) == MVT::Flag) {
69 SDOperand FlagVal(N, N->getNumValues()-1);
70
71 // There are either zero or one users of the Flag result.
72 bool HasFlagUse = false;
73 for (SDNode::use_iterator UI = N->use_begin(), E = N->use_end();
74 UI != E; ++UI)
75 if (FlagVal.isOperand(*UI)) {
76 HasFlagUse = true;
77 NodeSUnit->FlaggedNodes.push_back(N);
78 SUnitMap[N] = NodeSUnit;
79 N = *UI;
80 break;
81 }
82 if (!HasFlagUse) break;
83 }
84
85 // Now all flagged nodes are in FlaggedNodes and N is the bottom-most node.
86 // Update the SUnit
87 NodeSUnit->Node = N;
88 SUnitMap[N] = NodeSUnit;
89
90 // Compute the latency for the node. We use the sum of the latencies for
91 // all nodes flagged together into this SUnit.
92 if (InstrItins.isEmpty()) {
93 // No latency information.
94 NodeSUnit->Latency = 1;
95 } else {
96 NodeSUnit->Latency = 0;
97 if (N->isTargetOpcode()) {
98 unsigned SchedClass = TII->getSchedClass(N->getTargetOpcode());
99 InstrStage *S = InstrItins.begin(SchedClass);
100 InstrStage *E = InstrItins.end(SchedClass);
101 for (; S != E; ++S)
102 NodeSUnit->Latency += S->Cycles;
103 }
104 for (unsigned i = 0, e = NodeSUnit->FlaggedNodes.size(); i != e; ++i) {
105 SDNode *FNode = NodeSUnit->FlaggedNodes[i];
106 if (FNode->isTargetOpcode()) {
107 unsigned SchedClass = TII->getSchedClass(FNode->getTargetOpcode());
108 InstrStage *S = InstrItins.begin(SchedClass);
109 InstrStage *E = InstrItins.end(SchedClass);
110 for (; S != E; ++S)
111 NodeSUnit->Latency += S->Cycles;
112 }
113 }
114 }
115 }
116
117 // Pass 2: add the preds, succs, etc.
118 for (unsigned su = 0, e = SUnits.size(); su != e; ++su) {
119 SUnit *SU = &SUnits[su];
120 SDNode *MainNode = SU->Node;
121
122 if (MainNode->isTargetOpcode()) {
123 unsigned Opc = MainNode->getTargetOpcode();
Evan Cheng13d41b92006-05-12 01:58:24 +0000124 if (TII->isTwoAddrInstr(Opc))
Evan Chenge165a782006-05-11 23:55:42 +0000125 SU->isTwoAddress = true;
Evan Cheng13d41b92006-05-12 01:58:24 +0000126 if (TII->isCommutableInstr(Opc))
127 SU->isCommutable = true;
Evan Chenge165a782006-05-11 23:55:42 +0000128 }
129
130 // Find all predecessors and successors of the group.
131 // Temporarily add N to make code simpler.
132 SU->FlaggedNodes.push_back(MainNode);
133
134 for (unsigned n = 0, e = SU->FlaggedNodes.size(); n != e; ++n) {
135 SDNode *N = SU->FlaggedNodes[n];
136
137 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
138 SDNode *OpN = N->getOperand(i).Val;
139 if (isPassiveNode(OpN)) continue; // Not scheduled.
140 SUnit *OpSU = SUnitMap[OpN];
141 assert(OpSU && "Node has no SUnit!");
142 if (OpSU == SU) continue; // In the same group.
143
144 MVT::ValueType OpVT = N->getOperand(i).getValueType();
145 assert(OpVT != MVT::Flag && "Flagged nodes should be in same sunit!");
146 bool isChain = OpVT == MVT::Other;
147
148 if (SU->Preds.insert(std::make_pair(OpSU, isChain)).second) {
149 if (!isChain) {
150 SU->NumPreds++;
151 SU->NumPredsLeft++;
152 } else {
153 SU->NumChainPredsLeft++;
154 }
155 }
156 if (OpSU->Succs.insert(std::make_pair(SU, isChain)).second) {
157 if (!isChain) {
158 OpSU->NumSuccs++;
159 OpSU->NumSuccsLeft++;
160 } else {
161 OpSU->NumChainSuccsLeft++;
162 }
163 }
164 }
165 }
166
167 // Remove MainNode from FlaggedNodes again.
168 SU->FlaggedNodes.pop_back();
169 }
170
171 return;
172}
173
Evan Cheng8820ad52006-05-13 08:22:24 +0000174static void CalculateDepths(SUnit *SU, unsigned Depth) {
175 if (SU->Depth == 0 || Depth > SU->Depth) {
Evan Cheng626da3d2006-05-12 06:05:18 +0000176 SU->Depth = Depth;
177 for (std::set<std::pair<SUnit*, bool> >::iterator I = SU->Succs.begin(),
178 E = SU->Succs.end(); I != E; ++I)
Evan Cheng8820ad52006-05-13 08:22:24 +0000179 CalculateDepths(I->first, Depth+1);
Evan Cheng626da3d2006-05-12 06:05:18 +0000180 }
Evan Chenge165a782006-05-11 23:55:42 +0000181}
182
183void ScheduleDAG::CalculateDepths() {
184 SUnit *Entry = SUnitMap[DAG.getEntryNode().Val];
Evan Cheng8820ad52006-05-13 08:22:24 +0000185 ::CalculateDepths(Entry, 0U);
Evan Chenge165a782006-05-11 23:55:42 +0000186 for (unsigned i = 0, e = SUnits.size(); i != e; ++i)
187 if (SUnits[i].Preds.size() == 0 && &SUnits[i] != Entry) {
Evan Cheng8820ad52006-05-13 08:22:24 +0000188 ::CalculateDepths(&SUnits[i], 0U);
Evan Chenge165a782006-05-11 23:55:42 +0000189 }
190}
191
192static void CalculateHeights(SUnit *SU, unsigned Height) {
Evan Cheng8820ad52006-05-13 08:22:24 +0000193 if (SU->Height == 0 || Height > SU->Height) {
Evan Cheng626da3d2006-05-12 06:05:18 +0000194 SU->Height = Height;
195 for (std::set<std::pair<SUnit*, bool> >::iterator I = SU->Preds.begin(),
196 E = SU->Preds.end(); I != E; ++I)
197 CalculateHeights(I->first, Height+1);
198 }
Evan Chenge165a782006-05-11 23:55:42 +0000199}
200void ScheduleDAG::CalculateHeights() {
201 SUnit *Root = SUnitMap[DAG.getRoot().Val];
202 ::CalculateHeights(Root, 0U);
203}
204
Jim Laskeye6b90fb2005-09-26 21:57:04 +0000205/// CountResults - The results of target nodes have register or immediate
206/// operands first, then an optional chain, and optional flag operands (which do
207/// not go into the machine instrs.)
Evan Chenga9c20912006-01-21 02:32:06 +0000208static unsigned CountResults(SDNode *Node) {
Jim Laskeyb6d4c2c2005-09-30 19:15:27 +0000209 unsigned N = Node->getNumValues();
210 while (N && Node->getValueType(N - 1) == MVT::Flag)
Jim Laskeye6b90fb2005-09-26 21:57:04 +0000211 --N;
Jim Laskeyb6d4c2c2005-09-30 19:15:27 +0000212 if (N && Node->getValueType(N - 1) == MVT::Other)
Jim Laskeye6b90fb2005-09-26 21:57:04 +0000213 --N; // Skip over chain result.
214 return N;
215}
216
217/// CountOperands The inputs to target nodes have any actual inputs first,
218/// followed by an optional chain operand, then flag operands. Compute the
219/// number of actual operands that will go into the machine instr.
Evan Chenga9c20912006-01-21 02:32:06 +0000220static unsigned CountOperands(SDNode *Node) {
Jim Laskeyb6d4c2c2005-09-30 19:15:27 +0000221 unsigned N = Node->getNumOperands();
222 while (N && Node->getOperand(N - 1).getValueType() == MVT::Flag)
Jim Laskeye6b90fb2005-09-26 21:57:04 +0000223 --N;
Jim Laskeyb6d4c2c2005-09-30 19:15:27 +0000224 if (N && Node->getOperand(N - 1).getValueType() == MVT::Other)
Jim Laskeye6b90fb2005-09-26 21:57:04 +0000225 --N; // Ignore chain if it exists.
226 return N;
227}
228
Jim Laskey60f09922006-07-21 20:57:35 +0000229static const TargetRegisterClass *getInstrOperandRegClass(
230 const MRegisterInfo *MRI,
231 const TargetInstrInfo *TII,
232 const TargetInstrDescriptor *II,
233 unsigned Op) {
234 if (Op >= II->numOperands) {
235 assert((II->Flags & M_VARIABLE_OPS)&& "Invalid operand # of instruction");
236 return NULL;
237 }
238 const TargetOperandInfo &toi = II->OpInfo[Op];
239 return (toi.Flags & M_LOOK_UP_PTR_REG_CLASS)
240 ? TII->getPointerRegClass() : MRI->getRegClass(toi.RegClass);
241}
242
243static unsigned CreateVirtualRegisters(const MRegisterInfo *MRI,
244 MachineInstr *MI,
Evan Cheng4ef10862006-01-23 07:01:07 +0000245 unsigned NumResults,
246 SSARegMap *RegMap,
Evan Cheng21d03f22006-05-18 20:42:07 +0000247 const TargetInstrInfo *TII,
Evan Cheng4ef10862006-01-23 07:01:07 +0000248 const TargetInstrDescriptor &II) {
Jim Laskeye6b90fb2005-09-26 21:57:04 +0000249 // Create the result registers for this node and add the result regs to
250 // the machine instruction.
Evan Cheng21d03f22006-05-18 20:42:07 +0000251 unsigned ResultReg =
Jim Laskey60f09922006-07-21 20:57:35 +0000252 RegMap->createVirtualRegister(getInstrOperandRegClass(MRI, TII, &II, 0));
Jim Laskeye6b90fb2005-09-26 21:57:04 +0000253 MI->addRegOperand(ResultReg, MachineOperand::Def);
254 for (unsigned i = 1; i != NumResults; ++i) {
Jim Laskey60f09922006-07-21 20:57:35 +0000255 const TargetRegisterClass *RC = getInstrOperandRegClass(MRI, TII, &II, i);
Evan Cheng21d03f22006-05-18 20:42:07 +0000256 assert(RC && "Isn't a register operand!");
257 MI->addRegOperand(RegMap->createVirtualRegister(RC), MachineOperand::Def);
Jim Laskeye6b90fb2005-09-26 21:57:04 +0000258 }
259 return ResultReg;
260}
261
Chris Lattnerdf375062006-03-10 07:25:12 +0000262/// getVR - Return the virtual register corresponding to the specified result
263/// of the specified node.
264static unsigned getVR(SDOperand Op, std::map<SDNode*, unsigned> &VRBaseMap) {
265 std::map<SDNode*, unsigned>::iterator I = VRBaseMap.find(Op.Val);
266 assert(I != VRBaseMap.end() && "Node emitted out of order - late");
267 return I->second + Op.ResNo;
268}
269
270
Chris Lattnered18b682006-02-24 18:54:03 +0000271/// AddOperand - Add the specified operand to the specified machine instr. II
272/// specifies the instruction information for the node, and IIOpNum is the
273/// operand number (in the II) that we are adding. IIOpNum and II are used for
274/// assertions only.
275void ScheduleDAG::AddOperand(MachineInstr *MI, SDOperand Op,
276 unsigned IIOpNum,
Chris Lattnerdf375062006-03-10 07:25:12 +0000277 const TargetInstrDescriptor *II,
278 std::map<SDNode*, unsigned> &VRBaseMap) {
Chris Lattnered18b682006-02-24 18:54:03 +0000279 if (Op.isTargetOpcode()) {
280 // Note that this case is redundant with the final else block, but we
281 // include it because it is the most common and it makes the logic
282 // simpler here.
283 assert(Op.getValueType() != MVT::Other &&
284 Op.getValueType() != MVT::Flag &&
285 "Chain and flag operands should occur at end of operand list!");
286
287 // Get/emit the operand.
Chris Lattnerdf375062006-03-10 07:25:12 +0000288 unsigned VReg = getVR(Op, VRBaseMap);
Chris Lattnered18b682006-02-24 18:54:03 +0000289 MI->addRegOperand(VReg, MachineOperand::Use);
290
291 // Verify that it is right.
292 assert(MRegisterInfo::isVirtualRegister(VReg) && "Not a vreg?");
293 if (II) {
Jim Laskey60f09922006-07-21 20:57:35 +0000294 const TargetRegisterClass *RC =
295 getInstrOperandRegClass(MRI, TII, II, IIOpNum);
Evan Cheng21d03f22006-05-18 20:42:07 +0000296 assert(RC && "Don't have operand info for this instruction!");
297 assert(RegMap->getRegClass(VReg) == RC &&
Chris Lattnered18b682006-02-24 18:54:03 +0000298 "Register class of operand and regclass of use don't agree!");
299 }
300 } else if (ConstantSDNode *C =
301 dyn_cast<ConstantSDNode>(Op)) {
Chris Lattner2d90ac72006-05-04 18:05:43 +0000302 MI->addImmOperand(C->getValue());
Chris Lattnered18b682006-02-24 18:54:03 +0000303 } else if (RegisterSDNode*R =
304 dyn_cast<RegisterSDNode>(Op)) {
305 MI->addRegOperand(R->getReg(), MachineOperand::Use);
306 } else if (GlobalAddressSDNode *TGA =
307 dyn_cast<GlobalAddressSDNode>(Op)) {
Chris Lattnerea50fab2006-05-04 01:15:02 +0000308 MI->addGlobalAddressOperand(TGA->getGlobal(), TGA->getOffset());
Chris Lattnered18b682006-02-24 18:54:03 +0000309 } else if (BasicBlockSDNode *BB =
310 dyn_cast<BasicBlockSDNode>(Op)) {
311 MI->addMachineBasicBlockOperand(BB->getBasicBlock());
312 } else if (FrameIndexSDNode *FI =
313 dyn_cast<FrameIndexSDNode>(Op)) {
314 MI->addFrameIndexOperand(FI->getIndex());
Nate Begeman37efe672006-04-22 18:53:45 +0000315 } else if (JumpTableSDNode *JT =
316 dyn_cast<JumpTableSDNode>(Op)) {
317 MI->addJumpTableIndexOperand(JT->getIndex());
Chris Lattnered18b682006-02-24 18:54:03 +0000318 } else if (ConstantPoolSDNode *CP =
319 dyn_cast<ConstantPoolSDNode>(Op)) {
Evan Cheng404cb4f2006-02-25 09:54:52 +0000320 int Offset = CP->getOffset();
Chris Lattnered18b682006-02-24 18:54:03 +0000321 unsigned Align = CP->getAlignment();
322 // MachineConstantPool wants an explicit alignment.
323 if (Align == 0) {
324 if (CP->get()->getType() == Type::DoubleTy)
325 Align = 3; // always 8-byte align doubles.
Chris Lattner54a30b92006-03-20 01:51:46 +0000326 else {
Chris Lattnered18b682006-02-24 18:54:03 +0000327 Align = TM.getTargetData()
Owen Andersona69571c2006-05-03 01:29:57 +0000328 ->getTypeAlignmentShift(CP->get()->getType());
Chris Lattner54a30b92006-03-20 01:51:46 +0000329 if (Align == 0) {
330 // Alignment of packed types. FIXME!
Owen Andersona69571c2006-05-03 01:29:57 +0000331 Align = TM.getTargetData()->getTypeSize(CP->get()->getType());
Chris Lattner54a30b92006-03-20 01:51:46 +0000332 Align = Log2_64(Align);
333 }
334 }
Chris Lattnered18b682006-02-24 18:54:03 +0000335 }
336
337 unsigned Idx = ConstPool->getConstantPoolIndex(CP->get(), Align);
Evan Cheng404cb4f2006-02-25 09:54:52 +0000338 MI->addConstantPoolIndexOperand(Idx, Offset);
Chris Lattnered18b682006-02-24 18:54:03 +0000339 } else if (ExternalSymbolSDNode *ES =
340 dyn_cast<ExternalSymbolSDNode>(Op)) {
Chris Lattnerea50fab2006-05-04 01:15:02 +0000341 MI->addExternalSymbolOperand(ES->getSymbol());
Chris Lattnered18b682006-02-24 18:54:03 +0000342 } else {
343 assert(Op.getValueType() != MVT::Other &&
344 Op.getValueType() != MVT::Flag &&
345 "Chain and flag operands should occur at end of operand list!");
Chris Lattnerdf375062006-03-10 07:25:12 +0000346 unsigned VReg = getVR(Op, VRBaseMap);
Chris Lattnered18b682006-02-24 18:54:03 +0000347 MI->addRegOperand(VReg, MachineOperand::Use);
348
349 // Verify that it is right.
350 assert(MRegisterInfo::isVirtualRegister(VReg) && "Not a vreg?");
351 if (II) {
Jim Laskey60f09922006-07-21 20:57:35 +0000352 const TargetRegisterClass *RC =
353 getInstrOperandRegClass(MRI, TII, II, IIOpNum);
Evan Cheng21d03f22006-05-18 20:42:07 +0000354 assert(RC && "Don't have operand info for this instruction!");
355 assert(RegMap->getRegClass(VReg) == RC &&
Chris Lattnered18b682006-02-24 18:54:03 +0000356 "Register class of operand and regclass of use don't agree!");
357 }
358 }
359
360}
361
362
Jim Laskeyb6d4c2c2005-09-30 19:15:27 +0000363/// EmitNode - Generate machine code for an node and needed dependencies.
Jim Laskeye6b90fb2005-09-26 21:57:04 +0000364///
Chris Lattner8c7ef052006-03-10 07:28:36 +0000365void ScheduleDAG::EmitNode(SDNode *Node,
Chris Lattnerdf375062006-03-10 07:25:12 +0000366 std::map<SDNode*, unsigned> &VRBaseMap) {
Jim Laskeyb6d4c2c2005-09-30 19:15:27 +0000367 unsigned VRBase = 0; // First virtual register for node
Chris Lattner2d973e42005-08-18 20:07:59 +0000368
Jim Laskeyb6d4c2c2005-09-30 19:15:27 +0000369 // If machine instruction
370 if (Node->isTargetOpcode()) {
371 unsigned Opc = Node->getTargetOpcode();
Evan Chenga9c20912006-01-21 02:32:06 +0000372 const TargetInstrDescriptor &II = TII->get(Opc);
Chris Lattner2d973e42005-08-18 20:07:59 +0000373
Jim Laskeyb6d4c2c2005-09-30 19:15:27 +0000374 unsigned NumResults = CountResults(Node);
375 unsigned NodeOperands = CountOperands(Node);
Jim Laskeye6b90fb2005-09-26 21:57:04 +0000376 unsigned NumMIOperands = NodeOperands + NumResults;
Chris Lattnerda8abb02005-09-01 18:44:10 +0000377#ifndef NDEBUG
Evan Cheng8d3af5e2006-06-15 07:22:16 +0000378 assert((unsigned(II.numOperands) == NumMIOperands ||
379 (II.Flags & M_VARIABLE_OPS)) &&
Chris Lattner2d973e42005-08-18 20:07:59 +0000380 "#operands for dag node doesn't match .td file!");
Chris Lattnerca6aa2f2005-08-19 01:01:34 +0000381#endif
Chris Lattner2d973e42005-08-18 20:07:59 +0000382
383 // Create the new machine instruction.
Chris Lattner8b915b42006-05-04 18:16:01 +0000384 MachineInstr *MI = new MachineInstr(Opc, NumMIOperands);
Chris Lattner2d973e42005-08-18 20:07:59 +0000385
386 // Add result register values for things that are defined by this
387 // instruction.
Chris Lattnera4176522005-10-30 18:54:27 +0000388
389 // If the node is only used by a CopyToReg and the dest reg is a vreg, use
390 // the CopyToReg'd destination register instead of creating a new vreg.
391 if (NumResults == 1) {
392 for (SDNode::use_iterator UI = Node->use_begin(), E = Node->use_end();
393 UI != E; ++UI) {
394 SDNode *Use = *UI;
395 if (Use->getOpcode() == ISD::CopyToReg &&
396 Use->getOperand(2).Val == Node) {
397 unsigned Reg = cast<RegisterSDNode>(Use->getOperand(1))->getReg();
398 if (MRegisterInfo::isVirtualRegister(Reg)) {
399 VRBase = Reg;
400 MI->addRegOperand(Reg, MachineOperand::Def);
401 break;
402 }
403 }
404 }
405 }
406
407 // Otherwise, create new virtual registers.
408 if (NumResults && VRBase == 0)
Jim Laskey60f09922006-07-21 20:57:35 +0000409 VRBase = CreateVirtualRegisters(MRI, MI, NumResults, RegMap, TII, II);
Jim Laskeyb6d4c2c2005-09-30 19:15:27 +0000410
411 // Emit all of the actual operands of this instruction, adding them to the
412 // instruction as appropriate.
Chris Lattnered18b682006-02-24 18:54:03 +0000413 for (unsigned i = 0; i != NodeOperands; ++i)
Chris Lattnerdf375062006-03-10 07:25:12 +0000414 AddOperand(MI, Node->getOperand(i), i+NumResults, &II, VRBaseMap);
Evan Cheng13d41b92006-05-12 01:58:24 +0000415
416 // Commute node if it has been determined to be profitable.
417 if (CommuteSet.count(Node)) {
418 MachineInstr *NewMI = TII->commuteInstruction(MI);
419 if (NewMI == 0)
420 DEBUG(std::cerr << "Sched: COMMUTING FAILED!\n");
421 else {
422 DEBUG(std::cerr << "Sched: COMMUTED TO: " << *NewMI);
Evan Cheng4c6f2f92006-05-31 18:03:39 +0000423 if (MI != NewMI) {
424 delete MI;
425 MI = NewMI;
426 }
Evan Cheng13d41b92006-05-12 01:58:24 +0000427 }
428 }
429
Jim Laskeyb6d4c2c2005-09-30 19:15:27 +0000430 // Now that we have emitted all operands, emit this instruction itself.
431 if ((II.Flags & M_USES_CUSTOM_DAG_SCHED_INSERTION) == 0) {
432 BB->insert(BB->end(), MI);
433 } else {
434 // Insert this instruction into the end of the basic block, potentially
435 // taking some custom action.
436 BB = DAG.getTargetLoweringInfo().InsertAtEndOfBasicBlock(MI, BB);
437 }
438 } else {
439 switch (Node->getOpcode()) {
440 default:
Jim Laskey16d42c62006-07-11 18:25:13 +0000441#ifndef NDEBUG
442 Node->dump();
443#endif
Jim Laskeyb6d4c2c2005-09-30 19:15:27 +0000444 assert(0 && "This target-independent node should have been selected!");
445 case ISD::EntryToken: // fall thru
446 case ISD::TokenFactor:
447 break;
448 case ISD::CopyToReg: {
Chris Lattnerdf375062006-03-10 07:25:12 +0000449 unsigned InReg = getVR(Node->getOperand(2), VRBaseMap);
Chris Lattnera4176522005-10-30 18:54:27 +0000450 unsigned DestReg = cast<RegisterSDNode>(Node->getOperand(1))->getReg();
Chris Lattner45053fc2006-03-24 07:15:07 +0000451 if (InReg != DestReg) // Coalesced away the copy?
Evan Chenga9c20912006-01-21 02:32:06 +0000452 MRI->copyRegToReg(*BB, BB->end(), DestReg, InReg,
453 RegMap->getRegClass(InReg));
Jim Laskeyb6d4c2c2005-09-30 19:15:27 +0000454 break;
455 }
456 case ISD::CopyFromReg: {
457 unsigned SrcReg = cast<RegisterSDNode>(Node->getOperand(1))->getReg();
Chris Lattner089c25c2005-10-09 05:58:56 +0000458 if (MRegisterInfo::isVirtualRegister(SrcReg)) {
459 VRBase = SrcReg; // Just use the input register directly!
460 break;
461 }
462
Chris Lattnera4176522005-10-30 18:54:27 +0000463 // If the node is only used by a CopyToReg and the dest reg is a vreg, use
464 // the CopyToReg'd destination register instead of creating a new vreg.
465 for (SDNode::use_iterator UI = Node->use_begin(), E = Node->use_end();
466 UI != E; ++UI) {
467 SDNode *Use = *UI;
468 if (Use->getOpcode() == ISD::CopyToReg &&
469 Use->getOperand(2).Val == Node) {
470 unsigned DestReg = cast<RegisterSDNode>(Use->getOperand(1))->getReg();
471 if (MRegisterInfo::isVirtualRegister(DestReg)) {
472 VRBase = DestReg;
473 break;
474 }
475 }
476 }
477
Jim Laskeyb6d4c2c2005-09-30 19:15:27 +0000478 // Figure out the register class to create for the destreg.
479 const TargetRegisterClass *TRC = 0;
Chris Lattnera4176522005-10-30 18:54:27 +0000480 if (VRBase) {
481 TRC = RegMap->getRegClass(VRBase);
482 } else {
Chris Lattner089c25c2005-10-09 05:58:56 +0000483
Chris Lattnera4176522005-10-30 18:54:27 +0000484 // Pick the register class of the right type that contains this physreg.
Evan Chenga9c20912006-01-21 02:32:06 +0000485 for (MRegisterInfo::regclass_iterator I = MRI->regclass_begin(),
486 E = MRI->regclass_end(); I != E; ++I)
Nate Begeman6510b222005-12-01 04:51:06 +0000487 if ((*I)->hasType(Node->getValueType(0)) &&
Chris Lattnera4176522005-10-30 18:54:27 +0000488 (*I)->contains(SrcReg)) {
489 TRC = *I;
490 break;
491 }
492 assert(TRC && "Couldn't find register class for reg copy!");
Jim Laskeyb6d4c2c2005-09-30 19:15:27 +0000493
Chris Lattnera4176522005-10-30 18:54:27 +0000494 // Create the reg, emit the copy.
495 VRBase = RegMap->createVirtualRegister(TRC);
496 }
Evan Chenga9c20912006-01-21 02:32:06 +0000497 MRI->copyRegToReg(*BB, BB->end(), VRBase, SrcReg, TRC);
Jim Laskeyb6d4c2c2005-09-30 19:15:27 +0000498 break;
499 }
Chris Lattneracc43bf2006-01-26 23:28:04 +0000500 case ISD::INLINEASM: {
501 unsigned NumOps = Node->getNumOperands();
502 if (Node->getOperand(NumOps-1).getValueType() == MVT::Flag)
503 --NumOps; // Ignore the flag operand.
504
505 // Create the inline asm machine instruction.
506 MachineInstr *MI =
507 new MachineInstr(BB, TargetInstrInfo::INLINEASM, (NumOps-2)/2+1);
508
509 // Add the asm string as an external symbol operand.
510 const char *AsmStr =
511 cast<ExternalSymbolSDNode>(Node->getOperand(1))->getSymbol();
Chris Lattnerea50fab2006-05-04 01:15:02 +0000512 MI->addExternalSymbolOperand(AsmStr);
Chris Lattneracc43bf2006-01-26 23:28:04 +0000513
514 // Add all of the operand registers to the instruction.
Chris Lattnerc3a9f8d2006-02-23 19:21:04 +0000515 for (unsigned i = 2; i != NumOps;) {
516 unsigned Flags = cast<ConstantSDNode>(Node->getOperand(i))->getValue();
Chris Lattnerfd6d2822006-02-24 19:18:20 +0000517 unsigned NumVals = Flags >> 3;
Chris Lattnerc3a9f8d2006-02-23 19:21:04 +0000518
Chris Lattner2d90ac72006-05-04 18:05:43 +0000519 MI->addImmOperand(Flags);
Chris Lattnerc3a9f8d2006-02-23 19:21:04 +0000520 ++i; // Skip the ID value.
521
522 switch (Flags & 7) {
Chris Lattneracc43bf2006-01-26 23:28:04 +0000523 default: assert(0 && "Bad flags!");
Chris Lattnerc3a9f8d2006-02-23 19:21:04 +0000524 case 1: // Use of register.
Chris Lattnerfd6d2822006-02-24 19:18:20 +0000525 for (; NumVals; --NumVals, ++i) {
Chris Lattnerc3a9f8d2006-02-23 19:21:04 +0000526 unsigned Reg = cast<RegisterSDNode>(Node->getOperand(i))->getReg();
Chris Lattnerea50fab2006-05-04 01:15:02 +0000527 MI->addRegOperand(Reg, MachineOperand::Use);
Chris Lattnerc3a9f8d2006-02-23 19:21:04 +0000528 }
Chris Lattnerdc19b702006-02-04 02:26:14 +0000529 break;
Chris Lattnerc3a9f8d2006-02-23 19:21:04 +0000530 case 2: // Def of register.
Chris Lattnerfd6d2822006-02-24 19:18:20 +0000531 for (; NumVals; --NumVals, ++i) {
Chris Lattnerc3a9f8d2006-02-23 19:21:04 +0000532 unsigned Reg = cast<RegisterSDNode>(Node->getOperand(i))->getReg();
Chris Lattnerea50fab2006-05-04 01:15:02 +0000533 MI->addRegOperand(Reg, MachineOperand::Def);
Chris Lattnerc3a9f8d2006-02-23 19:21:04 +0000534 }
Chris Lattnerdc19b702006-02-04 02:26:14 +0000535 break;
Chris Lattnerdc19b702006-02-04 02:26:14 +0000536 case 3: { // Immediate.
Chris Lattnerfd6d2822006-02-24 19:18:20 +0000537 assert(NumVals == 1 && "Unknown immediate value!");
Chris Lattnerdc19b702006-02-04 02:26:14 +0000538 uint64_t Val = cast<ConstantSDNode>(Node->getOperand(i))->getValue();
Chris Lattner2d90ac72006-05-04 18:05:43 +0000539 MI->addImmOperand(Val);
Chris Lattnerc3a9f8d2006-02-23 19:21:04 +0000540 ++i;
Chris Lattnerdc19b702006-02-04 02:26:14 +0000541 break;
542 }
Chris Lattnerfd6d2822006-02-24 19:18:20 +0000543 case 4: // Addressing mode.
544 // The addressing mode has been selected, just add all of the
545 // operands to the machine instruction.
546 for (; NumVals; --NumVals, ++i)
Chris Lattnerdf375062006-03-10 07:25:12 +0000547 AddOperand(MI, Node->getOperand(i), 0, 0, VRBaseMap);
Chris Lattnerfd6d2822006-02-24 19:18:20 +0000548 break;
Chris Lattnerdc19b702006-02-04 02:26:14 +0000549 }
Chris Lattneracc43bf2006-01-26 23:28:04 +0000550 }
551 break;
552 }
Jim Laskeyb6d4c2c2005-09-30 19:15:27 +0000553 }
554 }
555
Chris Lattnerdf375062006-03-10 07:25:12 +0000556 assert(!VRBaseMap.count(Node) && "Node emitted out of order - early");
557 VRBaseMap[Node] = VRBase;
Jim Laskeyb6d4c2c2005-09-30 19:15:27 +0000558}
559
Chris Lattnera93dfcd2006-03-05 23:51:47 +0000560void ScheduleDAG::EmitNoop() {
561 TII->insertNoop(*BB, BB->end());
562}
563
Evan Chenge165a782006-05-11 23:55:42 +0000564/// EmitSchedule - Emit the machine code in scheduled order.
565void ScheduleDAG::EmitSchedule() {
Chris Lattner96645412006-05-16 06:10:58 +0000566 // If this is the first basic block in the function, and if it has live ins
567 // that need to be copied into vregs, emit the copies into the top of the
568 // block before emitting the code for the block.
569 MachineFunction &MF = DAG.getMachineFunction();
570 if (&MF.front() == BB && MF.livein_begin() != MF.livein_end()) {
571 for (MachineFunction::livein_iterator LI = MF.livein_begin(),
572 E = MF.livein_end(); LI != E; ++LI)
573 if (LI->second)
574 MRI->copyRegToReg(*MF.begin(), MF.begin()->end(), LI->second,
575 LI->first, RegMap->getRegClass(LI->second));
576 }
577
578
579 // Finally, emit the code for all of the scheduled instructions.
Evan Chenge165a782006-05-11 23:55:42 +0000580 std::map<SDNode*, unsigned> VRBaseMap;
581 for (unsigned i = 0, e = Sequence.size(); i != e; i++) {
582 if (SUnit *SU = Sequence[i]) {
583 for (unsigned j = 0, ee = SU->FlaggedNodes.size(); j != ee; j++)
584 EmitNode(SU->FlaggedNodes[j], VRBaseMap);
585 EmitNode(SU->Node, VRBaseMap);
586 } else {
587 // Null SUnit* is a noop.
588 EmitNoop();
589 }
590 }
591}
592
593/// dump - dump the schedule.
594void ScheduleDAG::dumpSchedule() const {
595 for (unsigned i = 0, e = Sequence.size(); i != e; i++) {
596 if (SUnit *SU = Sequence[i])
597 SU->dump(&DAG);
598 else
599 std::cerr << "**** NOOP ****\n";
600 }
601}
602
603
Evan Chenga9c20912006-01-21 02:32:06 +0000604/// Run - perform scheduling.
605///
606MachineBasicBlock *ScheduleDAG::Run() {
607 TII = TM.getInstrInfo();
608 MRI = TM.getRegisterInfo();
609 RegMap = BB->getParent()->getSSARegMap();
610 ConstPool = BB->getParent()->getConstantPool();
Evan Cheng4ef10862006-01-23 07:01:07 +0000611
Evan Chenga9c20912006-01-21 02:32:06 +0000612 Schedule();
613 return BB;
Chris Lattnerd32b2362005-08-18 18:45:24 +0000614}
Evan Cheng4ef10862006-01-23 07:01:07 +0000615
Evan Chenge165a782006-05-11 23:55:42 +0000616/// SUnit - Scheduling unit. It's an wrapper around either a single SDNode or
617/// a group of nodes flagged together.
618void SUnit::dump(const SelectionDAG *G) const {
619 std::cerr << "SU(" << NodeNum << "): ";
620 Node->dump(G);
621 std::cerr << "\n";
622 if (FlaggedNodes.size() != 0) {
623 for (unsigned i = 0, e = FlaggedNodes.size(); i != e; i++) {
624 std::cerr << " ";
625 FlaggedNodes[i]->dump(G);
626 std::cerr << "\n";
627 }
628 }
629}
Evan Cheng4ef10862006-01-23 07:01:07 +0000630
Evan Chenge165a782006-05-11 23:55:42 +0000631void SUnit::dumpAll(const SelectionDAG *G) const {
632 dump(G);
633
634 std::cerr << " # preds left : " << NumPredsLeft << "\n";
635 std::cerr << " # succs left : " << NumSuccsLeft << "\n";
636 std::cerr << " # chain preds left : " << NumChainPredsLeft << "\n";
637 std::cerr << " # chain succs left : " << NumChainSuccsLeft << "\n";
638 std::cerr << " Latency : " << Latency << "\n";
639 std::cerr << " Depth : " << Depth << "\n";
640 std::cerr << " Height : " << Height << "\n";
641
642 if (Preds.size() != 0) {
643 std::cerr << " Predecessors:\n";
644 for (std::set<std::pair<SUnit*,bool> >::const_iterator I = Preds.begin(),
645 E = Preds.end(); I != E; ++I) {
646 if (I->second)
647 std::cerr << " ch ";
648 else
649 std::cerr << " val ";
650 I->first->dump(G);
651 }
652 }
653 if (Succs.size() != 0) {
654 std::cerr << " Successors:\n";
655 for (std::set<std::pair<SUnit*, bool> >::const_iterator I = Succs.begin(),
656 E = Succs.end(); I != E; ++I) {
657 if (I->second)
658 std::cerr << " ch ";
659 else
660 std::cerr << " val ";
661 I->first->dump(G);
662 }
663 }
664 std::cerr << "\n";
665}