blob: 1b53beddf7671a8e7c09e738c3cdbbb7584e8635 [file] [log] [blame]
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001//===---- ScheduleDAG.cpp - Implement the ScheduleDAG class ---------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner081ce942007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007//
8//===----------------------------------------------------------------------===//
9//
10// 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.
13//
14//===----------------------------------------------------------------------===//
15
16#define DEBUG_TYPE "pre-RA-sched"
17#include "llvm/Type.h"
18#include "llvm/CodeGen/ScheduleDAG.h"
19#include "llvm/CodeGen/MachineConstantPool.h"
20#include "llvm/CodeGen/MachineFunction.h"
Chris Lattner1b989192007-12-31 04:13:23 +000021#include "llvm/CodeGen/MachineRegisterInfo.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000022#include "llvm/Target/TargetData.h"
23#include "llvm/Target/TargetMachine.h"
24#include "llvm/Target/TargetInstrInfo.h"
25#include "llvm/Target/TargetLowering.h"
26#include "llvm/Support/Debug.h"
27#include "llvm/Support/MathExtras.h"
28using namespace llvm;
29
Chris Lattner1b989192007-12-31 04:13:23 +000030ScheduleDAG::ScheduleDAG(SelectionDAG &dag, MachineBasicBlock *bb,
31 const TargetMachine &tm)
32 : DAG(dag), BB(bb), TM(tm), RegInfo(BB->getParent()->getRegInfo()) {
33 TII = TM.getInstrInfo();
Evan Cheng2d373922008-01-30 19:35:32 +000034 MF = &DAG.getMachineFunction();
Chris Lattner1b989192007-12-31 04:13:23 +000035 MRI = TM.getRegisterInfo();
36 ConstPool = BB->getParent()->getConstantPool();
37}
Evan Cheng93f143e2007-09-25 01:54:36 +000038
Evan Cheng93f143e2007-09-25 01:54:36 +000039/// CheckForPhysRegDependency - Check if the dependency between def and use of
40/// a specified operand is a physical register dependency. If so, returns the
41/// register and the cost of copying the register.
42static void CheckForPhysRegDependency(SDNode *Def, SDNode *Use, unsigned Op,
43 const MRegisterInfo *MRI,
44 const TargetInstrInfo *TII,
45 unsigned &PhysReg, int &Cost) {
46 if (Op != 2 || Use->getOpcode() != ISD::CopyToReg)
47 return;
48
49 unsigned Reg = cast<RegisterSDNode>(Use->getOperand(1))->getReg();
50 if (MRegisterInfo::isVirtualRegister(Reg))
51 return;
52
53 unsigned ResNo = Use->getOperand(2).ResNo;
54 if (Def->isTargetOpcode()) {
Chris Lattner5b930372008-01-07 07:27:27 +000055 const TargetInstrDesc &II = TII->get(Def->getTargetOpcode());
Chris Lattner0c2a4f32008-01-07 03:13:06 +000056 if (ResNo >= II.getNumDefs() &&
57 II.ImplicitDefs[ResNo - II.getNumDefs()] == Reg) {
Evan Cheng93f143e2007-09-25 01:54:36 +000058 PhysReg = Reg;
59 const TargetRegisterClass *RC =
Evan Cheng5ec4b762007-09-26 21:36:17 +000060 MRI->getPhysicalRegisterRegClass(Def->getValueType(ResNo), Reg);
Evan Cheng93f143e2007-09-25 01:54:36 +000061 Cost = RC->getCopyCost();
62 }
63 }
64}
65
66SUnit *ScheduleDAG::Clone(SUnit *Old) {
67 SUnit *SU = NewSUnit(Old->Node);
68 for (unsigned i = 0, e = SU->FlaggedNodes.size(); i != e; ++i)
69 SU->FlaggedNodes.push_back(SU->FlaggedNodes[i]);
70 SU->InstanceNo = SUnitMap[Old->Node].size();
71 SU->Latency = Old->Latency;
72 SU->isTwoAddress = Old->isTwoAddress;
73 SU->isCommutable = Old->isCommutable;
Evan Chengba597da2007-09-28 22:32:30 +000074 SU->hasPhysRegDefs = Old->hasPhysRegDefs;
Evan Cheng93f143e2007-09-25 01:54:36 +000075 SUnitMap[Old->Node].push_back(SU);
76 return SU;
77}
78
Evan Chengdd3f8b92007-10-05 01:39:18 +000079
Dan Gohmanf17a25c2007-07-18 16:29:46 +000080/// BuildSchedUnits - Build SUnits from the selection dag that we are input.
81/// This SUnit graph is similar to the SelectionDAG, but represents flagged
82/// together nodes with a single SUnit.
83void ScheduleDAG::BuildSchedUnits() {
84 // Reserve entries in the vector for each of the SUnits we are creating. This
85 // ensure that reallocation of the vector won't happen, so SUnit*'s won't get
86 // invalidated.
87 SUnits.reserve(std::distance(DAG.allnodes_begin(), DAG.allnodes_end()));
88
Dan Gohmanf17a25c2007-07-18 16:29:46 +000089 for (SelectionDAG::allnodes_iterator NI = DAG.allnodes_begin(),
90 E = DAG.allnodes_end(); NI != E; ++NI) {
91 if (isPassiveNode(NI)) // Leaf node, e.g. a TargetImmediate.
92 continue;
93
94 // If this node has already been processed, stop now.
Evan Cheng93f143e2007-09-25 01:54:36 +000095 if (SUnitMap[NI].size()) continue;
Dan Gohmanf17a25c2007-07-18 16:29:46 +000096
97 SUnit *NodeSUnit = NewSUnit(NI);
98
99 // See if anything is flagged to this node, if so, add them to flagged
100 // nodes. Nodes can have at most one flag input and one flag output. Flags
101 // are required the be the last operand and result of a node.
102
103 // Scan up, adding flagged preds to FlaggedNodes.
104 SDNode *N = NI;
105 if (N->getNumOperands() &&
106 N->getOperand(N->getNumOperands()-1).getValueType() == MVT::Flag) {
107 do {
108 N = N->getOperand(N->getNumOperands()-1).Val;
109 NodeSUnit->FlaggedNodes.push_back(N);
Evan Cheng93f143e2007-09-25 01:54:36 +0000110 SUnitMap[N].push_back(NodeSUnit);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000111 } while (N->getNumOperands() &&
112 N->getOperand(N->getNumOperands()-1).getValueType()== MVT::Flag);
113 std::reverse(NodeSUnit->FlaggedNodes.begin(),
114 NodeSUnit->FlaggedNodes.end());
115 }
116
117 // Scan down, adding this node and any flagged succs to FlaggedNodes if they
118 // have a user of the flag operand.
119 N = NI;
120 while (N->getValueType(N->getNumValues()-1) == MVT::Flag) {
121 SDOperand FlagVal(N, N->getNumValues()-1);
122
123 // There are either zero or one users of the Flag result.
124 bool HasFlagUse = false;
125 for (SDNode::use_iterator UI = N->use_begin(), E = N->use_end();
126 UI != E; ++UI)
127 if (FlagVal.isOperand(*UI)) {
128 HasFlagUse = true;
129 NodeSUnit->FlaggedNodes.push_back(N);
Evan Cheng93f143e2007-09-25 01:54:36 +0000130 SUnitMap[N].push_back(NodeSUnit);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000131 N = *UI;
132 break;
133 }
134 if (!HasFlagUse) break;
135 }
136
137 // Now all flagged nodes are in FlaggedNodes and N is the bottom-most node.
138 // Update the SUnit
139 NodeSUnit->Node = N;
Evan Cheng93f143e2007-09-25 01:54:36 +0000140 SUnitMap[N].push_back(NodeSUnit);
Evan Chengdd3f8b92007-10-05 01:39:18 +0000141
142 ComputeLatency(NodeSUnit);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000143 }
144
145 // Pass 2: add the preds, succs, etc.
146 for (unsigned su = 0, e = SUnits.size(); su != e; ++su) {
147 SUnit *SU = &SUnits[su];
148 SDNode *MainNode = SU->Node;
149
150 if (MainNode->isTargetOpcode()) {
151 unsigned Opc = MainNode->getTargetOpcode();
Chris Lattner5b930372008-01-07 07:27:27 +0000152 const TargetInstrDesc &TID = TII->get(Opc);
Chris Lattner0c2a4f32008-01-07 03:13:06 +0000153 for (unsigned i = 0; i != TID.getNumOperands(); ++i) {
Evan Cheng93f143e2007-09-25 01:54:36 +0000154 if (TID.getOperandConstraint(i, TOI::TIED_TO) != -1) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000155 SU->isTwoAddress = true;
156 break;
157 }
158 }
Chris Lattnerd8529ab2008-01-07 06:42:05 +0000159 if (TID.isCommutable())
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000160 SU->isCommutable = true;
161 }
162
163 // Find all predecessors and successors of the group.
164 // Temporarily add N to make code simpler.
165 SU->FlaggedNodes.push_back(MainNode);
166
167 for (unsigned n = 0, e = SU->FlaggedNodes.size(); n != e; ++n) {
168 SDNode *N = SU->FlaggedNodes[n];
Evan Chengba597da2007-09-28 22:32:30 +0000169 if (N->isTargetOpcode() &&
Chris Lattner0c2a4f32008-01-07 03:13:06 +0000170 TII->get(N->getTargetOpcode()).getImplicitDefs() &&
171 CountResults(N) > TII->get(N->getTargetOpcode()).getNumDefs())
Evan Chengba597da2007-09-28 22:32:30 +0000172 SU->hasPhysRegDefs = true;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000173
174 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
175 SDNode *OpN = N->getOperand(i).Val;
176 if (isPassiveNode(OpN)) continue; // Not scheduled.
Evan Cheng93f143e2007-09-25 01:54:36 +0000177 SUnit *OpSU = SUnitMap[OpN].front();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000178 assert(OpSU && "Node has no SUnit!");
179 if (OpSU == SU) continue; // In the same group.
180
181 MVT::ValueType OpVT = N->getOperand(i).getValueType();
182 assert(OpVT != MVT::Flag && "Flagged nodes should be in same sunit!");
183 bool isChain = OpVT == MVT::Other;
Evan Cheng93f143e2007-09-25 01:54:36 +0000184
185 unsigned PhysReg = 0;
186 int Cost = 1;
187 // Determine if this is a physical register dependency.
188 CheckForPhysRegDependency(OpN, N, i, MRI, TII, PhysReg, Cost);
189 SU->addPred(OpSU, isChain, false, PhysReg, Cost);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000190 }
191 }
192
193 // Remove MainNode from FlaggedNodes again.
194 SU->FlaggedNodes.pop_back();
195 }
196
197 return;
198}
199
Evan Chengdd3f8b92007-10-05 01:39:18 +0000200void ScheduleDAG::ComputeLatency(SUnit *SU) {
201 const InstrItineraryData &InstrItins = TM.getInstrItineraryData();
202
203 // Compute the latency for the node. We use the sum of the latencies for
204 // all nodes flagged together into this SUnit.
205 if (InstrItins.isEmpty()) {
206 // No latency information.
207 SU->Latency = 1;
208 } else {
209 SU->Latency = 0;
210 if (SU->Node->isTargetOpcode()) {
Chris Lattner3d54fcd2008-01-07 02:46:03 +0000211 unsigned SchedClass =
212 TII->get(SU->Node->getTargetOpcode()).getSchedClass();
Evan Chengdd3f8b92007-10-05 01:39:18 +0000213 InstrStage *S = InstrItins.begin(SchedClass);
214 InstrStage *E = InstrItins.end(SchedClass);
215 for (; S != E; ++S)
216 SU->Latency += S->Cycles;
217 }
218 for (unsigned i = 0, e = SU->FlaggedNodes.size(); i != e; ++i) {
219 SDNode *FNode = SU->FlaggedNodes[i];
220 if (FNode->isTargetOpcode()) {
Chris Lattner3d54fcd2008-01-07 02:46:03 +0000221 unsigned SchedClass =TII->get(FNode->getTargetOpcode()).getSchedClass();
Evan Chengdd3f8b92007-10-05 01:39:18 +0000222 InstrStage *S = InstrItins.begin(SchedClass);
223 InstrStage *E = InstrItins.end(SchedClass);
224 for (; S != E; ++S)
225 SU->Latency += S->Cycles;
226 }
227 }
228 }
229}
230
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000231void ScheduleDAG::CalculateDepths() {
232 std::vector<std::pair<SUnit*, unsigned> > WorkList;
233 for (unsigned i = 0, e = SUnits.size(); i != e; ++i)
Dan Gohman301f4052008-01-29 13:02:09 +0000234 if (SUnits[i].Preds.empty())
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000235 WorkList.push_back(std::make_pair(&SUnits[i], 0U));
236
237 while (!WorkList.empty()) {
238 SUnit *SU = WorkList.back().first;
239 unsigned Depth = WorkList.back().second;
240 WorkList.pop_back();
241 if (SU->Depth == 0 || Depth > SU->Depth) {
242 SU->Depth = Depth;
243 for (SUnit::succ_iterator I = SU->Succs.begin(), E = SU->Succs.end();
244 I != E; ++I)
Evan Chenge7959472007-09-19 01:38:40 +0000245 WorkList.push_back(std::make_pair(I->Dep, Depth+1));
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000246 }
247 }
248}
249
250void ScheduleDAG::CalculateHeights() {
251 std::vector<std::pair<SUnit*, unsigned> > WorkList;
Evan Cheng93f143e2007-09-25 01:54:36 +0000252 SUnit *Root = SUnitMap[DAG.getRoot().Val].front();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000253 WorkList.push_back(std::make_pair(Root, 0U));
254
255 while (!WorkList.empty()) {
256 SUnit *SU = WorkList.back().first;
257 unsigned Height = WorkList.back().second;
258 WorkList.pop_back();
259 if (SU->Height == 0 || Height > SU->Height) {
260 SU->Height = Height;
261 for (SUnit::pred_iterator I = SU->Preds.begin(), E = SU->Preds.end();
262 I != E; ++I)
Evan Chenge7959472007-09-19 01:38:40 +0000263 WorkList.push_back(std::make_pair(I->Dep, Height+1));
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000264 }
265 }
266}
267
268/// CountResults - The results of target nodes have register or immediate
269/// operands first, then an optional chain, and optional flag operands (which do
270/// not go into the machine instrs.)
271unsigned ScheduleDAG::CountResults(SDNode *Node) {
272 unsigned N = Node->getNumValues();
273 while (N && Node->getValueType(N - 1) == MVT::Flag)
274 --N;
275 if (N && Node->getValueType(N - 1) == MVT::Other)
276 --N; // Skip over chain result.
277 return N;
278}
279
280/// CountOperands The inputs to target nodes have any actual inputs first,
281/// followed by an optional chain operand, then flag operands. Compute the
282/// number of actual operands that will go into the machine instr.
283unsigned ScheduleDAG::CountOperands(SDNode *Node) {
284 unsigned N = Node->getNumOperands();
285 while (N && Node->getOperand(N - 1).getValueType() == MVT::Flag)
286 --N;
287 if (N && Node->getOperand(N - 1).getValueType() == MVT::Other)
288 --N; // Ignore chain if it exists.
289 return N;
290}
291
292static const TargetRegisterClass *getInstrOperandRegClass(
293 const MRegisterInfo *MRI,
294 const TargetInstrInfo *TII,
Chris Lattner5b930372008-01-07 07:27:27 +0000295 const TargetInstrDesc &II,
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000296 unsigned Op) {
Chris Lattner5b930372008-01-07 07:27:27 +0000297 if (Op >= II.getNumOperands()) {
298 assert(II.isVariadic() && "Invalid operand # of instruction");
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000299 return NULL;
300 }
Chris Lattner5b930372008-01-07 07:27:27 +0000301 if (II.OpInfo[Op].isLookupPtrRegClass())
Chris Lattnereeedb482008-01-07 02:39:19 +0000302 return TII->getPointerRegClass();
Chris Lattner5b930372008-01-07 07:27:27 +0000303 return MRI->getRegClass(II.OpInfo[Op].RegClass);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000304}
305
Evan Cheng93f143e2007-09-25 01:54:36 +0000306void ScheduleDAG::EmitCopyFromReg(SDNode *Node, unsigned ResNo,
307 unsigned InstanceNo, unsigned SrcReg,
Evan Cheng26639782007-08-02 00:28:15 +0000308 DenseMap<SDOperand, unsigned> &VRBaseMap) {
309 unsigned VRBase = 0;
310 if (MRegisterInfo::isVirtualRegister(SrcReg)) {
311 // Just use the input register directly!
Evan Cheng93f143e2007-09-25 01:54:36 +0000312 if (InstanceNo > 0)
313 VRBaseMap.erase(SDOperand(Node, ResNo));
Evan Cheng26639782007-08-02 00:28:15 +0000314 bool isNew = VRBaseMap.insert(std::make_pair(SDOperand(Node,ResNo),SrcReg));
315 assert(isNew && "Node emitted out of order - early");
316 return;
317 }
318
319 // If the node is only used by a CopyToReg and the dest reg is a vreg, use
320 // the CopyToReg'd destination register instead of creating a new vreg.
Evan Cheng93f143e2007-09-25 01:54:36 +0000321 bool MatchReg = true;
Evan Cheng26639782007-08-02 00:28:15 +0000322 for (SDNode::use_iterator UI = Node->use_begin(), E = Node->use_end();
323 UI != E; ++UI) {
324 SDNode *Use = *UI;
Evan Cheng93f143e2007-09-25 01:54:36 +0000325 bool Match = true;
Evan Cheng26639782007-08-02 00:28:15 +0000326 if (Use->getOpcode() == ISD::CopyToReg &&
327 Use->getOperand(2).Val == Node &&
328 Use->getOperand(2).ResNo == ResNo) {
329 unsigned DestReg = cast<RegisterSDNode>(Use->getOperand(1))->getReg();
330 if (MRegisterInfo::isVirtualRegister(DestReg)) {
331 VRBase = DestReg;
Evan Cheng93f143e2007-09-25 01:54:36 +0000332 Match = false;
333 } else if (DestReg != SrcReg)
334 Match = false;
335 } else {
336 for (unsigned i = 0, e = Use->getNumOperands(); i != e; ++i) {
337 SDOperand Op = Use->getOperand(i);
Evan Cheng4f0345c2007-12-14 08:25:15 +0000338 if (Op.Val != Node || Op.ResNo != ResNo)
Evan Cheng93f143e2007-09-25 01:54:36 +0000339 continue;
340 MVT::ValueType VT = Node->getValueType(Op.ResNo);
341 if (VT != MVT::Other && VT != MVT::Flag)
342 Match = false;
Evan Cheng26639782007-08-02 00:28:15 +0000343 }
344 }
Evan Cheng93f143e2007-09-25 01:54:36 +0000345 MatchReg &= Match;
346 if (VRBase)
347 break;
Evan Cheng26639782007-08-02 00:28:15 +0000348 }
349
Evan Cheng26639782007-08-02 00:28:15 +0000350 const TargetRegisterClass *TRC = 0;
Evan Cheng93f143e2007-09-25 01:54:36 +0000351 // Figure out the register class to create for the destreg.
352 if (VRBase)
Chris Lattner1b989192007-12-31 04:13:23 +0000353 TRC = RegInfo.getRegClass(VRBase);
Evan Cheng93f143e2007-09-25 01:54:36 +0000354 else
Evan Cheng5ec4b762007-09-26 21:36:17 +0000355 TRC = MRI->getPhysicalRegisterRegClass(Node->getValueType(ResNo), SrcReg);
Evan Cheng93f143e2007-09-25 01:54:36 +0000356
357 // If all uses are reading from the src physical register and copying the
358 // register is either impossible or very expensive, then don't create a copy.
359 if (MatchReg && TRC->getCopyCost() < 0) {
360 VRBase = SrcReg;
361 } else {
Evan Cheng26639782007-08-02 00:28:15 +0000362 // Create the reg, emit the copy.
Chris Lattner1b989192007-12-31 04:13:23 +0000363 VRBase = RegInfo.createVirtualRegister(TRC);
Owen Anderson8f2c8932007-12-31 06:32:00 +0000364 TII->copyRegToReg(*BB, BB->end(), VRBase, SrcReg, TRC, TRC);
Evan Cheng26639782007-08-02 00:28:15 +0000365 }
Evan Cheng26639782007-08-02 00:28:15 +0000366
Evan Cheng93f143e2007-09-25 01:54:36 +0000367 if (InstanceNo > 0)
368 VRBaseMap.erase(SDOperand(Node, ResNo));
Evan Cheng26639782007-08-02 00:28:15 +0000369 bool isNew = VRBaseMap.insert(std::make_pair(SDOperand(Node,ResNo), VRBase));
370 assert(isNew && "Node emitted out of order - early");
371}
372
373void ScheduleDAG::CreateVirtualRegisters(SDNode *Node,
374 MachineInstr *MI,
Chris Lattner5b930372008-01-07 07:27:27 +0000375 const TargetInstrDesc &II,
Evan Cheng26639782007-08-02 00:28:15 +0000376 DenseMap<SDOperand, unsigned> &VRBaseMap) {
Chris Lattner0c2a4f32008-01-07 03:13:06 +0000377 for (unsigned i = 0; i < II.getNumDefs(); ++i) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000378 // If the specific node value is only used by a CopyToReg and the dest reg
379 // is a vreg, use the CopyToReg'd destination register instead of creating
380 // a new vreg.
381 unsigned VRBase = 0;
382 for (SDNode::use_iterator UI = Node->use_begin(), E = Node->use_end();
383 UI != E; ++UI) {
384 SDNode *Use = *UI;
385 if (Use->getOpcode() == ISD::CopyToReg &&
386 Use->getOperand(2).Val == Node &&
387 Use->getOperand(2).ResNo == i) {
388 unsigned Reg = cast<RegisterSDNode>(Use->getOperand(1))->getReg();
389 if (MRegisterInfo::isVirtualRegister(Reg)) {
390 VRBase = Reg;
Chris Lattner63ab1f22007-12-30 00:41:17 +0000391 MI->addOperand(MachineOperand::CreateReg(Reg, true));
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000392 break;
393 }
394 }
395 }
396
Evan Cheng26639782007-08-02 00:28:15 +0000397 // Create the result registers for this node and add the result regs to
398 // the machine instruction.
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000399 if (VRBase == 0) {
Chris Lattner5b930372008-01-07 07:27:27 +0000400 const TargetRegisterClass *RC = getInstrOperandRegClass(MRI, TII, II, i);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000401 assert(RC && "Isn't a register operand!");
Chris Lattner1b989192007-12-31 04:13:23 +0000402 VRBase = RegInfo.createVirtualRegister(RC);
Chris Lattner63ab1f22007-12-30 00:41:17 +0000403 MI->addOperand(MachineOperand::CreateReg(VRBase, true));
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000404 }
405
406 bool isNew = VRBaseMap.insert(std::make_pair(SDOperand(Node,i), VRBase));
407 assert(isNew && "Node emitted out of order - early");
408 }
409}
410
411/// getVR - Return the virtual register corresponding to the specified result
412/// of the specified node.
413static unsigned getVR(SDOperand Op, DenseMap<SDOperand, unsigned> &VRBaseMap) {
414 DenseMap<SDOperand, unsigned>::iterator I = VRBaseMap.find(Op);
415 assert(I != VRBaseMap.end() && "Node emitted out of order - late");
416 return I->second;
417}
418
419
420/// AddOperand - Add the specified operand to the specified machine instr. II
421/// specifies the instruction information for the node, and IIOpNum is the
422/// operand number (in the II) that we are adding. IIOpNum and II are used for
423/// assertions only.
424void ScheduleDAG::AddOperand(MachineInstr *MI, SDOperand Op,
425 unsigned IIOpNum,
Chris Lattner5b930372008-01-07 07:27:27 +0000426 const TargetInstrDesc *II,
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000427 DenseMap<SDOperand, unsigned> &VRBaseMap) {
428 if (Op.isTargetOpcode()) {
429 // Note that this case is redundant with the final else block, but we
430 // include it because it is the most common and it makes the logic
431 // simpler here.
432 assert(Op.getValueType() != MVT::Other &&
433 Op.getValueType() != MVT::Flag &&
434 "Chain and flag operands should occur at end of operand list!");
435
436 // Get/emit the operand.
437 unsigned VReg = getVR(Op, VRBaseMap);
Chris Lattner5b930372008-01-07 07:27:27 +0000438 const TargetInstrDesc &TID = MI->getDesc();
439 bool isOptDef = (IIOpNum < TID.getNumOperands())
440 ? (TID.OpInfo[IIOpNum].isOptionalDef()) : false;
Chris Lattner63ab1f22007-12-30 00:41:17 +0000441 MI->addOperand(MachineOperand::CreateReg(VReg, isOptDef));
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000442
443 // Verify that it is right.
444 assert(MRegisterInfo::isVirtualRegister(VReg) && "Not a vreg?");
445 if (II) {
446 const TargetRegisterClass *RC =
Chris Lattner5b930372008-01-07 07:27:27 +0000447 getInstrOperandRegClass(MRI, TII, *II, IIOpNum);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000448 assert(RC && "Don't have operand info for this instruction!");
Chris Lattner1b989192007-12-31 04:13:23 +0000449 const TargetRegisterClass *VRC = RegInfo.getRegClass(VReg);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000450 if (VRC != RC) {
451 cerr << "Register class of operand and regclass of use don't agree!\n";
452#ifndef NDEBUG
453 cerr << "Operand = " << IIOpNum << "\n";
454 cerr << "Op->Val = "; Op.Val->dump(&DAG); cerr << "\n";
455 cerr << "MI = "; MI->print(cerr);
456 cerr << "VReg = " << VReg << "\n";
457 cerr << "VReg RegClass size = " << VRC->getSize()
458 << ", align = " << VRC->getAlignment() << "\n";
459 cerr << "Expected RegClass size = " << RC->getSize()
460 << ", align = " << RC->getAlignment() << "\n";
461#endif
462 cerr << "Fatal error, aborting.\n";
463 abort();
464 }
465 }
Chris Lattner8dfd3122007-12-30 00:51:11 +0000466 } else if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Chris Lattner63ab1f22007-12-30 00:41:17 +0000467 MI->addOperand(MachineOperand::CreateImm(C->getValue()));
Chris Lattner8dfd3122007-12-30 00:51:11 +0000468 } else if (RegisterSDNode *R = dyn_cast<RegisterSDNode>(Op)) {
Chris Lattner63ab1f22007-12-30 00:41:17 +0000469 MI->addOperand(MachineOperand::CreateReg(R->getReg(), false));
Chris Lattner8dfd3122007-12-30 00:51:11 +0000470 } else if (GlobalAddressSDNode *TGA = dyn_cast<GlobalAddressSDNode>(Op)) {
471 MI->addOperand(MachineOperand::CreateGA(TGA->getGlobal(),TGA->getOffset()));
472 } else if (BasicBlockSDNode *BB = dyn_cast<BasicBlockSDNode>(Op)) {
473 MI->addOperand(MachineOperand::CreateMBB(BB->getBasicBlock()));
474 } else if (FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(Op)) {
475 MI->addOperand(MachineOperand::CreateFI(FI->getIndex()));
476 } else if (JumpTableSDNode *JT = dyn_cast<JumpTableSDNode>(Op)) {
477 MI->addOperand(MachineOperand::CreateJTI(JT->getIndex()));
478 } else if (ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(Op)) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000479 int Offset = CP->getOffset();
480 unsigned Align = CP->getAlignment();
481 const Type *Type = CP->getType();
482 // MachineConstantPool wants an explicit alignment.
483 if (Align == 0) {
484 Align = TM.getTargetData()->getPreferredTypeAlignmentShift(Type);
485 if (Align == 0) {
486 // Alignment of vector types. FIXME!
Duncan Sandsf99fdc62007-11-01 20:53:16 +0000487 Align = TM.getTargetData()->getABITypeSize(Type);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000488 Align = Log2_64(Align);
489 }
490 }
491
492 unsigned Idx;
493 if (CP->isMachineConstantPoolEntry())
494 Idx = ConstPool->getConstantPoolIndex(CP->getMachineCPVal(), Align);
495 else
496 Idx = ConstPool->getConstantPoolIndex(CP->getConstVal(), Align);
Chris Lattner8dfd3122007-12-30 00:51:11 +0000497 MI->addOperand(MachineOperand::CreateCPI(Idx, Offset));
498 } else if (ExternalSymbolSDNode *ES = dyn_cast<ExternalSymbolSDNode>(Op)) {
499 MI->addOperand(MachineOperand::CreateES(ES->getSymbol()));
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000500 } else {
501 assert(Op.getValueType() != MVT::Other &&
502 Op.getValueType() != MVT::Flag &&
503 "Chain and flag operands should occur at end of operand list!");
504 unsigned VReg = getVR(Op, VRBaseMap);
Chris Lattner63ab1f22007-12-30 00:41:17 +0000505 MI->addOperand(MachineOperand::CreateReg(VReg, false));
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000506
507 // Verify that it is right.
508 assert(MRegisterInfo::isVirtualRegister(VReg) && "Not a vreg?");
509 if (II) {
510 const TargetRegisterClass *RC =
Chris Lattner5b930372008-01-07 07:27:27 +0000511 getInstrOperandRegClass(MRI, TII, *II, IIOpNum);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000512 assert(RC && "Don't have operand info for this instruction!");
Chris Lattner1b989192007-12-31 04:13:23 +0000513 assert(RegInfo.getRegClass(VReg) == RC &&
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000514 "Register class of operand and regclass of use don't agree!");
515 }
516 }
517
518}
519
Christopher Lambe95328d2007-07-26 08:12:07 +0000520// Returns the Register Class of a subregister
521static const TargetRegisterClass *getSubRegisterRegClass(
522 const TargetRegisterClass *TRC,
523 unsigned SubIdx) {
524 // Pick the register class of the subregister
525 MRegisterInfo::regclass_iterator I = TRC->subregclasses_begin() + SubIdx-1;
526 assert(I < TRC->subregclasses_end() &&
527 "Invalid subregister index for register class");
528 return *I;
529}
530
531static const TargetRegisterClass *getSuperregRegisterClass(
532 const TargetRegisterClass *TRC,
533 unsigned SubIdx,
534 MVT::ValueType VT) {
535 // Pick the register class of the superegister for this type
536 for (MRegisterInfo::regclass_iterator I = TRC->superregclasses_begin(),
537 E = TRC->superregclasses_end(); I != E; ++I)
538 if ((*I)->hasType(VT) && getSubRegisterRegClass(*I, SubIdx) == TRC)
539 return *I;
540 assert(false && "Couldn't find the register class");
541 return 0;
542}
543
544/// EmitSubregNode - Generate machine code for subreg nodes.
545///
546void ScheduleDAG::EmitSubregNode(SDNode *Node,
547 DenseMap<SDOperand, unsigned> &VRBaseMap) {
548 unsigned VRBase = 0;
549 unsigned Opc = Node->getTargetOpcode();
550 if (Opc == TargetInstrInfo::EXTRACT_SUBREG) {
551 // If the node is only used by a CopyToReg and the dest reg is a vreg, use
552 // the CopyToReg'd destination register instead of creating a new vreg.
553 for (SDNode::use_iterator UI = Node->use_begin(), E = Node->use_end();
554 UI != E; ++UI) {
555 SDNode *Use = *UI;
556 if (Use->getOpcode() == ISD::CopyToReg &&
557 Use->getOperand(2).Val == Node) {
558 unsigned DestReg = cast<RegisterSDNode>(Use->getOperand(1))->getReg();
559 if (MRegisterInfo::isVirtualRegister(DestReg)) {
560 VRBase = DestReg;
561 break;
562 }
563 }
564 }
565
566 unsigned SubIdx = cast<ConstantSDNode>(Node->getOperand(1))->getValue();
567
568 // TODO: If the node is a use of a CopyFromReg from a physical register
569 // fold the extract into the copy now
570
Christopher Lambe95328d2007-07-26 08:12:07 +0000571 // Create the extract_subreg machine instruction.
572 MachineInstr *MI =
573 new MachineInstr(BB, TII->get(TargetInstrInfo::EXTRACT_SUBREG));
574
575 // Figure out the register class to create for the destreg.
576 unsigned VReg = getVR(Node->getOperand(0), VRBaseMap);
Chris Lattner1b989192007-12-31 04:13:23 +0000577 const TargetRegisterClass *TRC = RegInfo.getRegClass(VReg);
Christopher Lambe95328d2007-07-26 08:12:07 +0000578 const TargetRegisterClass *SRC = getSubRegisterRegClass(TRC, SubIdx);
579
580 if (VRBase) {
581 // Grab the destination register
582 const TargetRegisterClass *DRC = 0;
Chris Lattner1b989192007-12-31 04:13:23 +0000583 DRC = RegInfo.getRegClass(VRBase);
Christopher Lambe95328d2007-07-26 08:12:07 +0000584 assert(SRC == DRC &&
585 "Source subregister and destination must have the same class");
586 } else {
587 // Create the reg
Chris Lattner1b989192007-12-31 04:13:23 +0000588 VRBase = RegInfo.createVirtualRegister(SRC);
Christopher Lambe95328d2007-07-26 08:12:07 +0000589 }
590
591 // Add def, source, and subreg index
Chris Lattner63ab1f22007-12-30 00:41:17 +0000592 MI->addOperand(MachineOperand::CreateReg(VRBase, true));
Christopher Lambe95328d2007-07-26 08:12:07 +0000593 AddOperand(MI, Node->getOperand(0), 0, 0, VRBaseMap);
Chris Lattner8dfd3122007-12-30 00:51:11 +0000594 MI->addOperand(MachineOperand::CreateImm(SubIdx));
Christopher Lambe95328d2007-07-26 08:12:07 +0000595
596 } else if (Opc == TargetInstrInfo::INSERT_SUBREG) {
597 assert((Node->getNumOperands() == 2 || Node->getNumOperands() == 3) &&
598 "Malformed insert_subreg node");
599 bool isUndefInput = (Node->getNumOperands() == 2);
600 unsigned SubReg = 0;
601 unsigned SubIdx = 0;
602
603 if (isUndefInput) {
604 SubReg = getVR(Node->getOperand(0), VRBaseMap);
605 SubIdx = cast<ConstantSDNode>(Node->getOperand(1))->getValue();
606 } else {
607 SubReg = getVR(Node->getOperand(1), VRBaseMap);
608 SubIdx = cast<ConstantSDNode>(Node->getOperand(2))->getValue();
609 }
610
Chris Lattnerb70e1512007-12-31 04:16:08 +0000611 // TODO: Add tracking info to MachineRegisterInfo of which vregs are subregs
Christopher Lambe95328d2007-07-26 08:12:07 +0000612 // to allow coalescing in the allocator
613
614 // If the node is only used by a CopyToReg and the dest reg is a vreg, use
615 // the CopyToReg'd destination register instead of creating a new vreg.
616 // If the CopyToReg'd destination register is physical, then fold the
617 // insert into the copy
618 for (SDNode::use_iterator UI = Node->use_begin(), E = Node->use_end();
619 UI != E; ++UI) {
620 SDNode *Use = *UI;
621 if (Use->getOpcode() == ISD::CopyToReg &&
622 Use->getOperand(2).Val == Node) {
623 unsigned DestReg = cast<RegisterSDNode>(Use->getOperand(1))->getReg();
624 if (MRegisterInfo::isVirtualRegister(DestReg)) {
625 VRBase = DestReg;
626 break;
627 }
628 }
629 }
630
631 // Create the insert_subreg machine instruction.
632 MachineInstr *MI =
633 new MachineInstr(BB, TII->get(TargetInstrInfo::INSERT_SUBREG));
634
635 // Figure out the register class to create for the destreg.
636 const TargetRegisterClass *TRC = 0;
637 if (VRBase) {
Chris Lattner1b989192007-12-31 04:13:23 +0000638 TRC = RegInfo.getRegClass(VRBase);
Christopher Lambe95328d2007-07-26 08:12:07 +0000639 } else {
Chris Lattner1b989192007-12-31 04:13:23 +0000640 TRC = getSuperregRegisterClass(RegInfo.getRegClass(SubReg), SubIdx,
Christopher Lambe95328d2007-07-26 08:12:07 +0000641 Node->getValueType(0));
642 assert(TRC && "Couldn't determine register class for insert_subreg");
Chris Lattner1b989192007-12-31 04:13:23 +0000643 VRBase = RegInfo.createVirtualRegister(TRC); // Create the reg
Christopher Lambe95328d2007-07-26 08:12:07 +0000644 }
645
Chris Lattner63ab1f22007-12-30 00:41:17 +0000646 MI->addOperand(MachineOperand::CreateReg(VRBase, true));
Christopher Lambe95328d2007-07-26 08:12:07 +0000647 AddOperand(MI, Node->getOperand(0), 0, 0, VRBaseMap);
648 if (!isUndefInput)
649 AddOperand(MI, Node->getOperand(1), 0, 0, VRBaseMap);
Chris Lattner8dfd3122007-12-30 00:51:11 +0000650 MI->addOperand(MachineOperand::CreateImm(SubIdx));
Christopher Lambe95328d2007-07-26 08:12:07 +0000651 } else
652 assert(0 && "Node is not a subreg insert or extract");
653
654 bool isNew = VRBaseMap.insert(std::make_pair(SDOperand(Node,0), VRBase));
655 assert(isNew && "Node emitted out of order - early");
656}
657
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000658/// EmitNode - Generate machine code for an node and needed dependencies.
659///
Evan Cheng93f143e2007-09-25 01:54:36 +0000660void ScheduleDAG::EmitNode(SDNode *Node, unsigned InstanceNo,
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000661 DenseMap<SDOperand, unsigned> &VRBaseMap) {
662 // If machine instruction
663 if (Node->isTargetOpcode()) {
664 unsigned Opc = Node->getTargetOpcode();
Christopher Lambe95328d2007-07-26 08:12:07 +0000665
666 // Handle subreg insert/extract specially
667 if (Opc == TargetInstrInfo::EXTRACT_SUBREG ||
668 Opc == TargetInstrInfo::INSERT_SUBREG) {
669 EmitSubregNode(Node, VRBaseMap);
670 return;
671 }
672
Chris Lattner5b930372008-01-07 07:27:27 +0000673 const TargetInstrDesc &II = TII->get(Opc);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000674
675 unsigned NumResults = CountResults(Node);
676 unsigned NodeOperands = CountOperands(Node);
677 unsigned NumMIOperands = NodeOperands + NumResults;
Chris Lattner0c2a4f32008-01-07 03:13:06 +0000678 bool HasPhysRegOuts = (NumResults > II.getNumDefs()) &&
679 II.getImplicitDefs() != 0;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000680#ifndef NDEBUG
Chris Lattner0c2a4f32008-01-07 03:13:06 +0000681 assert((II.getNumOperands() == NumMIOperands ||
Chris Lattner2fb37c02008-01-07 05:19:29 +0000682 HasPhysRegOuts || II.isVariadic()) &&
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000683 "#operands for dag node doesn't match .td file!");
684#endif
685
686 // Create the new machine instruction.
687 MachineInstr *MI = new MachineInstr(II);
688
689 // Add result register values for things that are defined by this
690 // instruction.
691 if (NumResults)
Evan Cheng26639782007-08-02 00:28:15 +0000692 CreateVirtualRegisters(Node, MI, II, VRBaseMap);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000693
694 // Emit all of the actual operands of this instruction, adding them to the
695 // instruction as appropriate.
696 for (unsigned i = 0; i != NodeOperands; ++i)
Chris Lattner0c2a4f32008-01-07 03:13:06 +0000697 AddOperand(MI, Node->getOperand(i), i+II.getNumDefs(), &II, VRBaseMap);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000698
699 // Commute node if it has been determined to be profitable.
700 if (CommuteSet.count(Node)) {
701 MachineInstr *NewMI = TII->commuteInstruction(MI);
702 if (NewMI == 0)
703 DOUT << "Sched: COMMUTING FAILED!\n";
704 else {
705 DOUT << "Sched: COMMUTED TO: " << *NewMI;
706 if (MI != NewMI) {
707 delete MI;
708 MI = NewMI;
709 }
710 }
711 }
712
713 // Now that we have emitted all operands, emit this instruction itself.
Evan Cheng2d373922008-01-30 19:35:32 +0000714 if (Opc == TargetInstrInfo::LABEL &&
715 !BB->empty() && &MF->front() == BB) {
716 // If we are inserting a LABEL and this happens to be the first label in
717 // the entry block, it is the "function start" label. Make sure there are
718 // no other instructions before it.
719 bool SeenLabel = false;
720 MachineBasicBlock::iterator MBBI = BB->begin();
721 while (MBBI != BB->end()) {
722 if (MBBI->getOpcode() == TargetInstrInfo::LABEL) {
723 SeenLabel = true;
724 break;
725 }
726 ++MBBI;
727 }
728 if (!SeenLabel)
729 BB->insert(BB->begin(), MI);
730 else
731 BB->push_back(MI);
732 } else if (II.usesCustomDAGSchedInsertionHook())
733 // Insert this instruction into the basic block using a target
734 // specific inserter which may returns a new basic block.
Evan Chenge637db12008-01-30 18:18:23 +0000735 BB = DAG.getTargetLoweringInfo().EmitInstrWithCustomInserter(MI, BB);
Evan Cheng2d373922008-01-30 19:35:32 +0000736 else
737 BB->push_back(MI);
Evan Cheng26639782007-08-02 00:28:15 +0000738
739 // Additional results must be an physical register def.
740 if (HasPhysRegOuts) {
Chris Lattner0c2a4f32008-01-07 03:13:06 +0000741 for (unsigned i = II.getNumDefs(); i < NumResults; ++i) {
742 unsigned Reg = II.getImplicitDefs()[i - II.getNumDefs()];
Evan Cheng0af04f72007-08-02 05:29:38 +0000743 if (Node->hasAnyUseOfValue(i))
Evan Cheng93f143e2007-09-25 01:54:36 +0000744 EmitCopyFromReg(Node, i, InstanceNo, Reg, VRBaseMap);
Evan Cheng26639782007-08-02 00:28:15 +0000745 }
746 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000747 } else {
748 switch (Node->getOpcode()) {
749 default:
750#ifndef NDEBUG
751 Node->dump(&DAG);
752#endif
753 assert(0 && "This target-independent node should have been selected!");
754 case ISD::EntryToken: // fall thru
755 case ISD::TokenFactor:
756 case ISD::LABEL:
757 break;
758 case ISD::CopyToReg: {
759 unsigned InReg;
760 if (RegisterSDNode *R = dyn_cast<RegisterSDNode>(Node->getOperand(2)))
761 InReg = R->getReg();
762 else
763 InReg = getVR(Node->getOperand(2), VRBaseMap);
764 unsigned DestReg = cast<RegisterSDNode>(Node->getOperand(1))->getReg();
765 if (InReg != DestReg) {// Coalesced away the copy?
766 const TargetRegisterClass *TRC = 0;
767 // Get the target register class
768 if (MRegisterInfo::isVirtualRegister(InReg))
Chris Lattner1b989192007-12-31 04:13:23 +0000769 TRC = RegInfo.getRegClass(InReg);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000770 else
Evan Cheng5ec4b762007-09-26 21:36:17 +0000771 TRC =
772 MRI->getPhysicalRegisterRegClass(Node->getOperand(2).getValueType(),
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000773 InReg);
Owen Anderson8f2c8932007-12-31 06:32:00 +0000774 TII->copyRegToReg(*BB, BB->end(), DestReg, InReg, TRC, TRC);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000775 }
776 break;
777 }
778 case ISD::CopyFromReg: {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000779 unsigned SrcReg = cast<RegisterSDNode>(Node->getOperand(1))->getReg();
Evan Cheng93f143e2007-09-25 01:54:36 +0000780 EmitCopyFromReg(Node, 0, InstanceNo, SrcReg, VRBaseMap);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000781 break;
782 }
783 case ISD::INLINEASM: {
784 unsigned NumOps = Node->getNumOperands();
785 if (Node->getOperand(NumOps-1).getValueType() == MVT::Flag)
786 --NumOps; // Ignore the flag operand.
787
788 // Create the inline asm machine instruction.
789 MachineInstr *MI =
790 new MachineInstr(BB, TII->get(TargetInstrInfo::INLINEASM));
791
792 // Add the asm string as an external symbol operand.
793 const char *AsmStr =
794 cast<ExternalSymbolSDNode>(Node->getOperand(1))->getSymbol();
Chris Lattner8dfd3122007-12-30 00:51:11 +0000795 MI->addOperand(MachineOperand::CreateES(AsmStr));
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000796
797 // Add all of the operand registers to the instruction.
798 for (unsigned i = 2; i != NumOps;) {
799 unsigned Flags = cast<ConstantSDNode>(Node->getOperand(i))->getValue();
800 unsigned NumVals = Flags >> 3;
801
Chris Lattner8dfd3122007-12-30 00:51:11 +0000802 MI->addOperand(MachineOperand::CreateImm(Flags));
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000803 ++i; // Skip the ID value.
804
805 switch (Flags & 7) {
806 default: assert(0 && "Bad flags!");
807 case 1: // Use of register.
808 for (; NumVals; --NumVals, ++i) {
809 unsigned Reg = cast<RegisterSDNode>(Node->getOperand(i))->getReg();
Chris Lattner63ab1f22007-12-30 00:41:17 +0000810 MI->addOperand(MachineOperand::CreateReg(Reg, false));
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000811 }
812 break;
813 case 2: // Def of register.
814 for (; NumVals; --NumVals, ++i) {
815 unsigned Reg = cast<RegisterSDNode>(Node->getOperand(i))->getReg();
Chris Lattner63ab1f22007-12-30 00:41:17 +0000816 MI->addOperand(MachineOperand::CreateReg(Reg, true));
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000817 }
818 break;
819 case 3: { // Immediate.
Chris Lattner23544c12007-08-25 00:53:07 +0000820 for (; NumVals; --NumVals, ++i) {
821 if (ConstantSDNode *CS =
822 dyn_cast<ConstantSDNode>(Node->getOperand(i))) {
Chris Lattner63ab1f22007-12-30 00:41:17 +0000823 MI->addOperand(MachineOperand::CreateImm(CS->getValue()));
Dale Johannesencfb19e62007-11-05 21:20:28 +0000824 } else if (GlobalAddressSDNode *GA =
825 dyn_cast<GlobalAddressSDNode>(Node->getOperand(i))) {
Chris Lattner8dfd3122007-12-30 00:51:11 +0000826 MI->addOperand(MachineOperand::CreateGA(GA->getGlobal(),
827 GA->getOffset()));
Dale Johannesencfb19e62007-11-05 21:20:28 +0000828 } else {
Chris Lattner8dfd3122007-12-30 00:51:11 +0000829 BasicBlockSDNode *BB =cast<BasicBlockSDNode>(Node->getOperand(i));
830 MI->addOperand(MachineOperand::CreateMBB(BB->getBasicBlock()));
Chris Lattner23544c12007-08-25 00:53:07 +0000831 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000832 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000833 break;
834 }
835 case 4: // Addressing mode.
836 // The addressing mode has been selected, just add all of the
837 // operands to the machine instruction.
838 for (; NumVals; --NumVals, ++i)
839 AddOperand(MI, Node->getOperand(i), 0, 0, VRBaseMap);
840 break;
841 }
842 }
843 break;
844 }
845 }
846 }
847}
848
849void ScheduleDAG::EmitNoop() {
850 TII->insertNoop(*BB, BB->end());
851}
852
Evan Cheng5ec4b762007-09-26 21:36:17 +0000853void ScheduleDAG::EmitCrossRCCopy(SUnit *SU, DenseMap<SUnit*, unsigned> &VRBaseMap) {
854 for (SUnit::const_pred_iterator I = SU->Preds.begin(), E = SU->Preds.end();
855 I != E; ++I) {
856 if (I->isCtrl) continue; // ignore chain preds
857 if (!I->Dep->Node) {
858 // Copy to physical register.
859 DenseMap<SUnit*, unsigned>::iterator VRI = VRBaseMap.find(I->Dep);
860 assert(VRI != VRBaseMap.end() && "Node emitted out of order - late");
861 // Find the destination physical register.
862 unsigned Reg = 0;
863 for (SUnit::const_succ_iterator II = SU->Succs.begin(),
864 EE = SU->Succs.end(); II != EE; ++II) {
865 if (I->Reg) {
866 Reg = I->Reg;
867 break;
868 }
869 }
870 assert(I->Reg && "Unknown physical register!");
Owen Anderson8f2c8932007-12-31 06:32:00 +0000871 TII->copyRegToReg(*BB, BB->end(), Reg, VRI->second,
Evan Cheng5ec4b762007-09-26 21:36:17 +0000872 SU->CopyDstRC, SU->CopySrcRC);
873 } else {
874 // Copy from physical register.
875 assert(I->Reg && "Unknown physical register!");
Chris Lattner1b989192007-12-31 04:13:23 +0000876 unsigned VRBase = RegInfo.createVirtualRegister(SU->CopyDstRC);
Evan Cheng5ec4b762007-09-26 21:36:17 +0000877 bool isNew = VRBaseMap.insert(std::make_pair(SU, VRBase));
878 assert(isNew && "Node emitted out of order - early");
Owen Anderson8f2c8932007-12-31 06:32:00 +0000879 TII->copyRegToReg(*BB, BB->end(), VRBase, I->Reg,
Evan Cheng5ec4b762007-09-26 21:36:17 +0000880 SU->CopyDstRC, SU->CopySrcRC);
881 }
882 break;
883 }
884}
885
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000886/// EmitSchedule - Emit the machine code in scheduled order.
887void ScheduleDAG::EmitSchedule() {
888 // If this is the first basic block in the function, and if it has live ins
889 // that need to be copied into vregs, emit the copies into the top of the
890 // block before emitting the code for the block.
Evan Cheng2d373922008-01-30 19:35:32 +0000891 if (&MF->front() == BB) {
Chris Lattner1b989192007-12-31 04:13:23 +0000892 for (MachineRegisterInfo::livein_iterator LI = RegInfo.livein_begin(),
893 E = RegInfo.livein_end(); LI != E; ++LI)
Evan Chengb3d91cf2007-09-26 06:25:56 +0000894 if (LI->second) {
Chris Lattner1b989192007-12-31 04:13:23 +0000895 const TargetRegisterClass *RC = RegInfo.getRegClass(LI->second);
Evan Cheng2d373922008-01-30 19:35:32 +0000896 TII->copyRegToReg(*MF->begin(), MF->begin()->end(), LI->second,
Evan Chengb3d91cf2007-09-26 06:25:56 +0000897 LI->first, RC, RC);
898 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000899 }
900
901
902 // Finally, emit the code for all of the scheduled instructions.
903 DenseMap<SDOperand, unsigned> VRBaseMap;
Evan Cheng5ec4b762007-09-26 21:36:17 +0000904 DenseMap<SUnit*, unsigned> CopyVRBaseMap;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000905 for (unsigned i = 0, e = Sequence.size(); i != e; i++) {
906 if (SUnit *SU = Sequence[i]) {
Evan Cheng93f143e2007-09-25 01:54:36 +0000907 for (unsigned j = 0, ee = SU->FlaggedNodes.size(); j != ee; ++j)
908 EmitNode(SU->FlaggedNodes[j], SU->InstanceNo, VRBaseMap);
Evan Cheng5ec4b762007-09-26 21:36:17 +0000909 if (SU->Node)
910 EmitNode(SU->Node, SU->InstanceNo, VRBaseMap);
911 else
912 EmitCrossRCCopy(SU, CopyVRBaseMap);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000913 } else {
914 // Null SUnit* is a noop.
915 EmitNoop();
916 }
917 }
918}
919
920/// dump - dump the schedule.
921void ScheduleDAG::dumpSchedule() const {
922 for (unsigned i = 0, e = Sequence.size(); i != e; i++) {
923 if (SUnit *SU = Sequence[i])
924 SU->dump(&DAG);
925 else
926 cerr << "**** NOOP ****\n";
927 }
928}
929
930
931/// Run - perform scheduling.
932///
933MachineBasicBlock *ScheduleDAG::Run() {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000934 Schedule();
935 return BB;
936}
937
938/// SUnit - Scheduling unit. It's an wrapper around either a single SDNode or
939/// a group of nodes flagged together.
940void SUnit::dump(const SelectionDAG *G) const {
941 cerr << "SU(" << NodeNum << "): ";
Evan Cheng5ec4b762007-09-26 21:36:17 +0000942 if (Node)
943 Node->dump(G);
944 else
945 cerr << "CROSS RC COPY ";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000946 cerr << "\n";
947 if (FlaggedNodes.size() != 0) {
948 for (unsigned i = 0, e = FlaggedNodes.size(); i != e; i++) {
949 cerr << " ";
950 FlaggedNodes[i]->dump(G);
951 cerr << "\n";
952 }
953 }
954}
955
956void SUnit::dumpAll(const SelectionDAG *G) const {
957 dump(G);
958
959 cerr << " # preds left : " << NumPredsLeft << "\n";
960 cerr << " # succs left : " << NumSuccsLeft << "\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000961 cerr << " Latency : " << Latency << "\n";
962 cerr << " Depth : " << Depth << "\n";
963 cerr << " Height : " << Height << "\n";
964
965 if (Preds.size() != 0) {
966 cerr << " Predecessors:\n";
967 for (SUnit::const_succ_iterator I = Preds.begin(), E = Preds.end();
968 I != E; ++I) {
Evan Chenge7959472007-09-19 01:38:40 +0000969 if (I->isCtrl)
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000970 cerr << " ch #";
971 else
972 cerr << " val #";
Evan Cheng93f143e2007-09-25 01:54:36 +0000973 cerr << I->Dep << " - SU(" << I->Dep->NodeNum << ")";
974 if (I->isSpecial)
975 cerr << " *";
976 cerr << "\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000977 }
978 }
979 if (Succs.size() != 0) {
980 cerr << " Successors:\n";
981 for (SUnit::const_succ_iterator I = Succs.begin(), E = Succs.end();
982 I != E; ++I) {
Evan Chenge7959472007-09-19 01:38:40 +0000983 if (I->isCtrl)
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000984 cerr << " ch #";
985 else
986 cerr << " val #";
Evan Cheng93f143e2007-09-25 01:54:36 +0000987 cerr << I->Dep << " - SU(" << I->Dep->NodeNum << ")";
988 if (I->isSpecial)
989 cerr << " *";
990 cerr << "\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000991 }
992 }
993 cerr << "\n";
994}