blob: 3873c33a20fae17a0c50453b5867ac7cdd6956da [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
Dan Gohmanf14b4472008-01-31 00:25:39 +0000280/// CountOperands - The inputs to target nodes have any actual inputs first,
281/// followed by optional memory operands chain operand, then flag operands.
282/// Compute the number of actual operands that will go into the machine istr.
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000283unsigned 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.
Dan Gohmanf14b4472008-01-31 00:25:39 +0000289 while (N && MemOperandSDNode::classof(Node->getOperand(N - 1).Val))
290 --N; // Ignore MemOperand nodes
291 return N;
292}
293
294/// CountMemOperands - Find the index of the last MemOperandSDNode operand
295unsigned ScheduleDAG::CountMemOperands(SDNode *Node) {
296 unsigned N = Node->getNumOperands();
297 while (N && Node->getOperand(N - 1).getValueType() == MVT::Flag)
298 --N;
299 if (N && Node->getOperand(N - 1).getValueType() == MVT::Other)
300 --N; // Ignore chain if it exists.
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000301 return N;
302}
303
304static const TargetRegisterClass *getInstrOperandRegClass(
305 const MRegisterInfo *MRI,
306 const TargetInstrInfo *TII,
Chris Lattner5b930372008-01-07 07:27:27 +0000307 const TargetInstrDesc &II,
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000308 unsigned Op) {
Chris Lattner5b930372008-01-07 07:27:27 +0000309 if (Op >= II.getNumOperands()) {
310 assert(II.isVariadic() && "Invalid operand # of instruction");
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000311 return NULL;
312 }
Chris Lattner5b930372008-01-07 07:27:27 +0000313 if (II.OpInfo[Op].isLookupPtrRegClass())
Chris Lattnereeedb482008-01-07 02:39:19 +0000314 return TII->getPointerRegClass();
Chris Lattner5b930372008-01-07 07:27:27 +0000315 return MRI->getRegClass(II.OpInfo[Op].RegClass);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000316}
317
Evan Cheng93f143e2007-09-25 01:54:36 +0000318void ScheduleDAG::EmitCopyFromReg(SDNode *Node, unsigned ResNo,
319 unsigned InstanceNo, unsigned SrcReg,
Evan Cheng26639782007-08-02 00:28:15 +0000320 DenseMap<SDOperand, unsigned> &VRBaseMap) {
321 unsigned VRBase = 0;
322 if (MRegisterInfo::isVirtualRegister(SrcReg)) {
323 // Just use the input register directly!
Evan Cheng93f143e2007-09-25 01:54:36 +0000324 if (InstanceNo > 0)
325 VRBaseMap.erase(SDOperand(Node, ResNo));
Evan Cheng26639782007-08-02 00:28:15 +0000326 bool isNew = VRBaseMap.insert(std::make_pair(SDOperand(Node,ResNo),SrcReg));
327 assert(isNew && "Node emitted out of order - early");
328 return;
329 }
330
331 // If the node is only used by a CopyToReg and the dest reg is a vreg, use
332 // the CopyToReg'd destination register instead of creating a new vreg.
Evan Cheng93f143e2007-09-25 01:54:36 +0000333 bool MatchReg = true;
Evan Cheng26639782007-08-02 00:28:15 +0000334 for (SDNode::use_iterator UI = Node->use_begin(), E = Node->use_end();
335 UI != E; ++UI) {
336 SDNode *Use = *UI;
Evan Cheng93f143e2007-09-25 01:54:36 +0000337 bool Match = true;
Evan Cheng26639782007-08-02 00:28:15 +0000338 if (Use->getOpcode() == ISD::CopyToReg &&
339 Use->getOperand(2).Val == Node &&
340 Use->getOperand(2).ResNo == ResNo) {
341 unsigned DestReg = cast<RegisterSDNode>(Use->getOperand(1))->getReg();
342 if (MRegisterInfo::isVirtualRegister(DestReg)) {
343 VRBase = DestReg;
Evan Cheng93f143e2007-09-25 01:54:36 +0000344 Match = false;
345 } else if (DestReg != SrcReg)
346 Match = false;
347 } else {
348 for (unsigned i = 0, e = Use->getNumOperands(); i != e; ++i) {
349 SDOperand Op = Use->getOperand(i);
Evan Cheng4f0345c2007-12-14 08:25:15 +0000350 if (Op.Val != Node || Op.ResNo != ResNo)
Evan Cheng93f143e2007-09-25 01:54:36 +0000351 continue;
352 MVT::ValueType VT = Node->getValueType(Op.ResNo);
353 if (VT != MVT::Other && VT != MVT::Flag)
354 Match = false;
Evan Cheng26639782007-08-02 00:28:15 +0000355 }
356 }
Evan Cheng93f143e2007-09-25 01:54:36 +0000357 MatchReg &= Match;
358 if (VRBase)
359 break;
Evan Cheng26639782007-08-02 00:28:15 +0000360 }
361
Evan Cheng26639782007-08-02 00:28:15 +0000362 const TargetRegisterClass *TRC = 0;
Evan Cheng93f143e2007-09-25 01:54:36 +0000363 // Figure out the register class to create for the destreg.
364 if (VRBase)
Chris Lattner1b989192007-12-31 04:13:23 +0000365 TRC = RegInfo.getRegClass(VRBase);
Evan Cheng93f143e2007-09-25 01:54:36 +0000366 else
Evan Cheng5ec4b762007-09-26 21:36:17 +0000367 TRC = MRI->getPhysicalRegisterRegClass(Node->getValueType(ResNo), SrcReg);
Evan Cheng93f143e2007-09-25 01:54:36 +0000368
369 // If all uses are reading from the src physical register and copying the
370 // register is either impossible or very expensive, then don't create a copy.
371 if (MatchReg && TRC->getCopyCost() < 0) {
372 VRBase = SrcReg;
373 } else {
Evan Cheng26639782007-08-02 00:28:15 +0000374 // Create the reg, emit the copy.
Chris Lattner1b989192007-12-31 04:13:23 +0000375 VRBase = RegInfo.createVirtualRegister(TRC);
Owen Anderson8f2c8932007-12-31 06:32:00 +0000376 TII->copyRegToReg(*BB, BB->end(), VRBase, SrcReg, TRC, TRC);
Evan Cheng26639782007-08-02 00:28:15 +0000377 }
Evan Cheng26639782007-08-02 00:28:15 +0000378
Evan Cheng93f143e2007-09-25 01:54:36 +0000379 if (InstanceNo > 0)
380 VRBaseMap.erase(SDOperand(Node, ResNo));
Evan Cheng26639782007-08-02 00:28:15 +0000381 bool isNew = VRBaseMap.insert(std::make_pair(SDOperand(Node,ResNo), VRBase));
382 assert(isNew && "Node emitted out of order - early");
383}
384
385void ScheduleDAG::CreateVirtualRegisters(SDNode *Node,
386 MachineInstr *MI,
Chris Lattner5b930372008-01-07 07:27:27 +0000387 const TargetInstrDesc &II,
Evan Cheng26639782007-08-02 00:28:15 +0000388 DenseMap<SDOperand, unsigned> &VRBaseMap) {
Chris Lattner0c2a4f32008-01-07 03:13:06 +0000389 for (unsigned i = 0; i < II.getNumDefs(); ++i) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000390 // If the specific node value is only used by a CopyToReg and the dest reg
391 // is a vreg, use the CopyToReg'd destination register instead of creating
392 // a new vreg.
393 unsigned VRBase = 0;
394 for (SDNode::use_iterator UI = Node->use_begin(), E = Node->use_end();
395 UI != E; ++UI) {
396 SDNode *Use = *UI;
397 if (Use->getOpcode() == ISD::CopyToReg &&
398 Use->getOperand(2).Val == Node &&
399 Use->getOperand(2).ResNo == i) {
400 unsigned Reg = cast<RegisterSDNode>(Use->getOperand(1))->getReg();
401 if (MRegisterInfo::isVirtualRegister(Reg)) {
402 VRBase = Reg;
Chris Lattner63ab1f22007-12-30 00:41:17 +0000403 MI->addOperand(MachineOperand::CreateReg(Reg, true));
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000404 break;
405 }
406 }
407 }
408
Evan Cheng26639782007-08-02 00:28:15 +0000409 // Create the result registers for this node and add the result regs to
410 // the machine instruction.
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000411 if (VRBase == 0) {
Chris Lattner5b930372008-01-07 07:27:27 +0000412 const TargetRegisterClass *RC = getInstrOperandRegClass(MRI, TII, II, i);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000413 assert(RC && "Isn't a register operand!");
Chris Lattner1b989192007-12-31 04:13:23 +0000414 VRBase = RegInfo.createVirtualRegister(RC);
Chris Lattner63ab1f22007-12-30 00:41:17 +0000415 MI->addOperand(MachineOperand::CreateReg(VRBase, true));
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000416 }
417
418 bool isNew = VRBaseMap.insert(std::make_pair(SDOperand(Node,i), VRBase));
419 assert(isNew && "Node emitted out of order - early");
420 }
421}
422
423/// getVR - Return the virtual register corresponding to the specified result
424/// of the specified node.
425static unsigned getVR(SDOperand Op, DenseMap<SDOperand, unsigned> &VRBaseMap) {
426 DenseMap<SDOperand, unsigned>::iterator I = VRBaseMap.find(Op);
427 assert(I != VRBaseMap.end() && "Node emitted out of order - late");
428 return I->second;
429}
430
431
432/// AddOperand - Add the specified operand to the specified machine instr. II
433/// specifies the instruction information for the node, and IIOpNum is the
434/// operand number (in the II) that we are adding. IIOpNum and II are used for
435/// assertions only.
436void ScheduleDAG::AddOperand(MachineInstr *MI, SDOperand Op,
437 unsigned IIOpNum,
Chris Lattner5b930372008-01-07 07:27:27 +0000438 const TargetInstrDesc *II,
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000439 DenseMap<SDOperand, unsigned> &VRBaseMap) {
440 if (Op.isTargetOpcode()) {
441 // Note that this case is redundant with the final else block, but we
442 // include it because it is the most common and it makes the logic
443 // simpler here.
444 assert(Op.getValueType() != MVT::Other &&
445 Op.getValueType() != MVT::Flag &&
446 "Chain and flag operands should occur at end of operand list!");
447
448 // Get/emit the operand.
449 unsigned VReg = getVR(Op, VRBaseMap);
Chris Lattner5b930372008-01-07 07:27:27 +0000450 const TargetInstrDesc &TID = MI->getDesc();
451 bool isOptDef = (IIOpNum < TID.getNumOperands())
452 ? (TID.OpInfo[IIOpNum].isOptionalDef()) : false;
Chris Lattner63ab1f22007-12-30 00:41:17 +0000453 MI->addOperand(MachineOperand::CreateReg(VReg, isOptDef));
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000454
455 // Verify that it is right.
456 assert(MRegisterInfo::isVirtualRegister(VReg) && "Not a vreg?");
457 if (II) {
458 const TargetRegisterClass *RC =
Chris Lattner5b930372008-01-07 07:27:27 +0000459 getInstrOperandRegClass(MRI, TII, *II, IIOpNum);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000460 assert(RC && "Don't have operand info for this instruction!");
Chris Lattner1b989192007-12-31 04:13:23 +0000461 const TargetRegisterClass *VRC = RegInfo.getRegClass(VReg);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000462 if (VRC != RC) {
463 cerr << "Register class of operand and regclass of use don't agree!\n";
464#ifndef NDEBUG
465 cerr << "Operand = " << IIOpNum << "\n";
466 cerr << "Op->Val = "; Op.Val->dump(&DAG); cerr << "\n";
467 cerr << "MI = "; MI->print(cerr);
468 cerr << "VReg = " << VReg << "\n";
469 cerr << "VReg RegClass size = " << VRC->getSize()
470 << ", align = " << VRC->getAlignment() << "\n";
471 cerr << "Expected RegClass size = " << RC->getSize()
472 << ", align = " << RC->getAlignment() << "\n";
473#endif
474 cerr << "Fatal error, aborting.\n";
475 abort();
476 }
477 }
Chris Lattner8dfd3122007-12-30 00:51:11 +0000478 } else if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Chris Lattner63ab1f22007-12-30 00:41:17 +0000479 MI->addOperand(MachineOperand::CreateImm(C->getValue()));
Chris Lattner8dfd3122007-12-30 00:51:11 +0000480 } else if (RegisterSDNode *R = dyn_cast<RegisterSDNode>(Op)) {
Chris Lattner63ab1f22007-12-30 00:41:17 +0000481 MI->addOperand(MachineOperand::CreateReg(R->getReg(), false));
Chris Lattner8dfd3122007-12-30 00:51:11 +0000482 } else if (GlobalAddressSDNode *TGA = dyn_cast<GlobalAddressSDNode>(Op)) {
483 MI->addOperand(MachineOperand::CreateGA(TGA->getGlobal(),TGA->getOffset()));
484 } else if (BasicBlockSDNode *BB = dyn_cast<BasicBlockSDNode>(Op)) {
485 MI->addOperand(MachineOperand::CreateMBB(BB->getBasicBlock()));
486 } else if (FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(Op)) {
487 MI->addOperand(MachineOperand::CreateFI(FI->getIndex()));
488 } else if (JumpTableSDNode *JT = dyn_cast<JumpTableSDNode>(Op)) {
489 MI->addOperand(MachineOperand::CreateJTI(JT->getIndex()));
490 } else if (ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(Op)) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000491 int Offset = CP->getOffset();
492 unsigned Align = CP->getAlignment();
493 const Type *Type = CP->getType();
494 // MachineConstantPool wants an explicit alignment.
495 if (Align == 0) {
496 Align = TM.getTargetData()->getPreferredTypeAlignmentShift(Type);
497 if (Align == 0) {
498 // Alignment of vector types. FIXME!
Duncan Sandsf99fdc62007-11-01 20:53:16 +0000499 Align = TM.getTargetData()->getABITypeSize(Type);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000500 Align = Log2_64(Align);
501 }
502 }
503
504 unsigned Idx;
505 if (CP->isMachineConstantPoolEntry())
506 Idx = ConstPool->getConstantPoolIndex(CP->getMachineCPVal(), Align);
507 else
508 Idx = ConstPool->getConstantPoolIndex(CP->getConstVal(), Align);
Chris Lattner8dfd3122007-12-30 00:51:11 +0000509 MI->addOperand(MachineOperand::CreateCPI(Idx, Offset));
510 } else if (ExternalSymbolSDNode *ES = dyn_cast<ExternalSymbolSDNode>(Op)) {
511 MI->addOperand(MachineOperand::CreateES(ES->getSymbol()));
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000512 } else {
513 assert(Op.getValueType() != MVT::Other &&
514 Op.getValueType() != MVT::Flag &&
515 "Chain and flag operands should occur at end of operand list!");
516 unsigned VReg = getVR(Op, VRBaseMap);
Chris Lattner63ab1f22007-12-30 00:41:17 +0000517 MI->addOperand(MachineOperand::CreateReg(VReg, false));
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000518
519 // Verify that it is right.
520 assert(MRegisterInfo::isVirtualRegister(VReg) && "Not a vreg?");
521 if (II) {
522 const TargetRegisterClass *RC =
Chris Lattner5b930372008-01-07 07:27:27 +0000523 getInstrOperandRegClass(MRI, TII, *II, IIOpNum);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000524 assert(RC && "Don't have operand info for this instruction!");
Chris Lattner1b989192007-12-31 04:13:23 +0000525 assert(RegInfo.getRegClass(VReg) == RC &&
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000526 "Register class of operand and regclass of use don't agree!");
527 }
528 }
529
530}
531
Dan Gohmanf14b4472008-01-31 00:25:39 +0000532void ScheduleDAG::AddMemOperand(MachineInstr *MI, const MemOperand &MO) {
533 MI->addMemOperand(MO);
534}
535
Christopher Lambe95328d2007-07-26 08:12:07 +0000536// Returns the Register Class of a subregister
537static const TargetRegisterClass *getSubRegisterRegClass(
538 const TargetRegisterClass *TRC,
539 unsigned SubIdx) {
540 // Pick the register class of the subregister
541 MRegisterInfo::regclass_iterator I = TRC->subregclasses_begin() + SubIdx-1;
542 assert(I < TRC->subregclasses_end() &&
543 "Invalid subregister index for register class");
544 return *I;
545}
546
547static const TargetRegisterClass *getSuperregRegisterClass(
548 const TargetRegisterClass *TRC,
549 unsigned SubIdx,
550 MVT::ValueType VT) {
551 // Pick the register class of the superegister for this type
552 for (MRegisterInfo::regclass_iterator I = TRC->superregclasses_begin(),
553 E = TRC->superregclasses_end(); I != E; ++I)
554 if ((*I)->hasType(VT) && getSubRegisterRegClass(*I, SubIdx) == TRC)
555 return *I;
556 assert(false && "Couldn't find the register class");
557 return 0;
558}
559
560/// EmitSubregNode - Generate machine code for subreg nodes.
561///
562void ScheduleDAG::EmitSubregNode(SDNode *Node,
563 DenseMap<SDOperand, unsigned> &VRBaseMap) {
564 unsigned VRBase = 0;
565 unsigned Opc = Node->getTargetOpcode();
566 if (Opc == TargetInstrInfo::EXTRACT_SUBREG) {
567 // If the node is only used by a CopyToReg and the dest reg is a vreg, use
568 // the CopyToReg'd destination register instead of creating a new vreg.
569 for (SDNode::use_iterator UI = Node->use_begin(), E = Node->use_end();
570 UI != E; ++UI) {
571 SDNode *Use = *UI;
572 if (Use->getOpcode() == ISD::CopyToReg &&
573 Use->getOperand(2).Val == Node) {
574 unsigned DestReg = cast<RegisterSDNode>(Use->getOperand(1))->getReg();
575 if (MRegisterInfo::isVirtualRegister(DestReg)) {
576 VRBase = DestReg;
577 break;
578 }
579 }
580 }
581
582 unsigned SubIdx = cast<ConstantSDNode>(Node->getOperand(1))->getValue();
583
584 // TODO: If the node is a use of a CopyFromReg from a physical register
585 // fold the extract into the copy now
586
Christopher Lambe95328d2007-07-26 08:12:07 +0000587 // Create the extract_subreg machine instruction.
588 MachineInstr *MI =
589 new MachineInstr(BB, TII->get(TargetInstrInfo::EXTRACT_SUBREG));
590
591 // Figure out the register class to create for the destreg.
592 unsigned VReg = getVR(Node->getOperand(0), VRBaseMap);
Chris Lattner1b989192007-12-31 04:13:23 +0000593 const TargetRegisterClass *TRC = RegInfo.getRegClass(VReg);
Christopher Lambe95328d2007-07-26 08:12:07 +0000594 const TargetRegisterClass *SRC = getSubRegisterRegClass(TRC, SubIdx);
595
596 if (VRBase) {
597 // Grab the destination register
598 const TargetRegisterClass *DRC = 0;
Chris Lattner1b989192007-12-31 04:13:23 +0000599 DRC = RegInfo.getRegClass(VRBase);
Christopher Lambe08d9ec2008-01-31 07:09:08 +0000600 assert(SRC && DRC && SRC == DRC &&
Christopher Lambe95328d2007-07-26 08:12:07 +0000601 "Source subregister and destination must have the same class");
602 } else {
603 // Create the reg
Christopher Lambe08d9ec2008-01-31 07:09:08 +0000604 assert(SRC && "Couldn't find source register class");
Chris Lattner1b989192007-12-31 04:13:23 +0000605 VRBase = RegInfo.createVirtualRegister(SRC);
Christopher Lambe95328d2007-07-26 08:12:07 +0000606 }
607
608 // Add def, source, and subreg index
Chris Lattner63ab1f22007-12-30 00:41:17 +0000609 MI->addOperand(MachineOperand::CreateReg(VRBase, true));
Christopher Lambe95328d2007-07-26 08:12:07 +0000610 AddOperand(MI, Node->getOperand(0), 0, 0, VRBaseMap);
Chris Lattner8dfd3122007-12-30 00:51:11 +0000611 MI->addOperand(MachineOperand::CreateImm(SubIdx));
Christopher Lambe95328d2007-07-26 08:12:07 +0000612
613 } else if (Opc == TargetInstrInfo::INSERT_SUBREG) {
614 assert((Node->getNumOperands() == 2 || Node->getNumOperands() == 3) &&
615 "Malformed insert_subreg node");
616 bool isUndefInput = (Node->getNumOperands() == 2);
617 unsigned SubReg = 0;
618 unsigned SubIdx = 0;
619
620 if (isUndefInput) {
621 SubReg = getVR(Node->getOperand(0), VRBaseMap);
622 SubIdx = cast<ConstantSDNode>(Node->getOperand(1))->getValue();
623 } else {
624 SubReg = getVR(Node->getOperand(1), VRBaseMap);
625 SubIdx = cast<ConstantSDNode>(Node->getOperand(2))->getValue();
626 }
627
Chris Lattnerb70e1512007-12-31 04:16:08 +0000628 // TODO: Add tracking info to MachineRegisterInfo of which vregs are subregs
Christopher Lambe95328d2007-07-26 08:12:07 +0000629 // to allow coalescing in the allocator
630
631 // If the node is only used by a CopyToReg and the dest reg is a vreg, use
632 // the CopyToReg'd destination register instead of creating a new vreg.
633 // If the CopyToReg'd destination register is physical, then fold the
634 // insert into the copy
635 for (SDNode::use_iterator UI = Node->use_begin(), E = Node->use_end();
636 UI != E; ++UI) {
637 SDNode *Use = *UI;
638 if (Use->getOpcode() == ISD::CopyToReg &&
639 Use->getOperand(2).Val == Node) {
640 unsigned DestReg = cast<RegisterSDNode>(Use->getOperand(1))->getReg();
641 if (MRegisterInfo::isVirtualRegister(DestReg)) {
642 VRBase = DestReg;
643 break;
644 }
645 }
646 }
647
648 // Create the insert_subreg machine instruction.
649 MachineInstr *MI =
650 new MachineInstr(BB, TII->get(TargetInstrInfo::INSERT_SUBREG));
651
652 // Figure out the register class to create for the destreg.
653 const TargetRegisterClass *TRC = 0;
654 if (VRBase) {
Chris Lattner1b989192007-12-31 04:13:23 +0000655 TRC = RegInfo.getRegClass(VRBase);
Christopher Lambe95328d2007-07-26 08:12:07 +0000656 } else {
Chris Lattner1b989192007-12-31 04:13:23 +0000657 TRC = getSuperregRegisterClass(RegInfo.getRegClass(SubReg), SubIdx,
Christopher Lambe95328d2007-07-26 08:12:07 +0000658 Node->getValueType(0));
659 assert(TRC && "Couldn't determine register class for insert_subreg");
Chris Lattner1b989192007-12-31 04:13:23 +0000660 VRBase = RegInfo.createVirtualRegister(TRC); // Create the reg
Christopher Lambe95328d2007-07-26 08:12:07 +0000661 }
662
Chris Lattner63ab1f22007-12-30 00:41:17 +0000663 MI->addOperand(MachineOperand::CreateReg(VRBase, true));
Christopher Lambe95328d2007-07-26 08:12:07 +0000664 AddOperand(MI, Node->getOperand(0), 0, 0, VRBaseMap);
665 if (!isUndefInput)
666 AddOperand(MI, Node->getOperand(1), 0, 0, VRBaseMap);
Chris Lattner8dfd3122007-12-30 00:51:11 +0000667 MI->addOperand(MachineOperand::CreateImm(SubIdx));
Christopher Lambe95328d2007-07-26 08:12:07 +0000668 } else
669 assert(0 && "Node is not a subreg insert or extract");
670
671 bool isNew = VRBaseMap.insert(std::make_pair(SDOperand(Node,0), VRBase));
672 assert(isNew && "Node emitted out of order - early");
673}
674
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000675/// EmitNode - Generate machine code for an node and needed dependencies.
676///
Evan Cheng93f143e2007-09-25 01:54:36 +0000677void ScheduleDAG::EmitNode(SDNode *Node, unsigned InstanceNo,
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000678 DenseMap<SDOperand, unsigned> &VRBaseMap) {
679 // If machine instruction
680 if (Node->isTargetOpcode()) {
681 unsigned Opc = Node->getTargetOpcode();
Christopher Lambe95328d2007-07-26 08:12:07 +0000682
683 // Handle subreg insert/extract specially
684 if (Opc == TargetInstrInfo::EXTRACT_SUBREG ||
685 Opc == TargetInstrInfo::INSERT_SUBREG) {
686 EmitSubregNode(Node, VRBaseMap);
687 return;
688 }
689
Chris Lattner5b930372008-01-07 07:27:27 +0000690 const TargetInstrDesc &II = TII->get(Opc);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000691
692 unsigned NumResults = CountResults(Node);
693 unsigned NodeOperands = CountOperands(Node);
Dan Gohmanf14b4472008-01-31 00:25:39 +0000694 unsigned NodeMemOperands = CountMemOperands(Node);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000695 unsigned NumMIOperands = NodeOperands + NumResults;
Chris Lattner0c2a4f32008-01-07 03:13:06 +0000696 bool HasPhysRegOuts = (NumResults > II.getNumDefs()) &&
697 II.getImplicitDefs() != 0;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000698#ifndef NDEBUG
Chris Lattner0c2a4f32008-01-07 03:13:06 +0000699 assert((II.getNumOperands() == NumMIOperands ||
Chris Lattner2fb37c02008-01-07 05:19:29 +0000700 HasPhysRegOuts || II.isVariadic()) &&
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000701 "#operands for dag node doesn't match .td file!");
702#endif
703
704 // Create the new machine instruction.
705 MachineInstr *MI = new MachineInstr(II);
706
707 // Add result register values for things that are defined by this
708 // instruction.
709 if (NumResults)
Evan Cheng26639782007-08-02 00:28:15 +0000710 CreateVirtualRegisters(Node, MI, II, VRBaseMap);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000711
712 // Emit all of the actual operands of this instruction, adding them to the
713 // instruction as appropriate.
714 for (unsigned i = 0; i != NodeOperands; ++i)
Chris Lattner0c2a4f32008-01-07 03:13:06 +0000715 AddOperand(MI, Node->getOperand(i), i+II.getNumDefs(), &II, VRBaseMap);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000716
Dan Gohmanf14b4472008-01-31 00:25:39 +0000717 // Emit all of the memory operands of this instruction
718 for (unsigned i = NodeOperands; i != NodeMemOperands; ++i)
719 AddMemOperand(MI, cast<MemOperandSDNode>(Node->getOperand(i))->MO);
720
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000721 // Commute node if it has been determined to be profitable.
722 if (CommuteSet.count(Node)) {
723 MachineInstr *NewMI = TII->commuteInstruction(MI);
724 if (NewMI == 0)
725 DOUT << "Sched: COMMUTING FAILED!\n";
726 else {
727 DOUT << "Sched: COMMUTED TO: " << *NewMI;
728 if (MI != NewMI) {
729 delete MI;
730 MI = NewMI;
731 }
732 }
733 }
734
735 // Now that we have emitted all operands, emit this instruction itself.
Evan Cheng13d1c292008-01-31 09:59:15 +0000736 if (ISD::isDebugLabel(Node) &&
Evan Cheng2d373922008-01-30 19:35:32 +0000737 !BB->empty() && &MF->front() == BB) {
Evan Cheng13d1c292008-01-31 09:59:15 +0000738 // If we are inserting a debug label and this happens to be the first
739 // debug label in the entry block, it is the "function start" label.
740 // Make sure there are no other instructions before it.
Evan Chenga7265852008-01-30 20:08:35 +0000741 unsigned NumLabels = 0;
Evan Cheng2d373922008-01-30 19:35:32 +0000742 MachineBasicBlock::iterator MBBI = BB->begin();
743 while (MBBI != BB->end()) {
Evan Cheng345235cc2008-01-31 10:05:13 +0000744 // FIXME: This is a nasty short term workaround. For now, we are
745 // assuming there are two debug labels at the beginning of the
746 // entry block: one for dbg_func_start, one for the first
747 // dbg_stoppoint before actual code.
Evan Cheng13d1c292008-01-31 09:59:15 +0000748 if (!MBBI->isDebugLabel() || ++NumLabels > 1)
749 break;
Evan Cheng2d373922008-01-30 19:35:32 +0000750 ++MBBI;
751 }
Evan Chenga7265852008-01-30 20:08:35 +0000752 if (NumLabels <= 1)
Evan Cheng2d373922008-01-30 19:35:32 +0000753 BB->insert(BB->begin(), MI);
754 else
755 BB->push_back(MI);
756 } else if (II.usesCustomDAGSchedInsertionHook())
757 // Insert this instruction into the basic block using a target
758 // specific inserter which may returns a new basic block.
Evan Chenge637db12008-01-30 18:18:23 +0000759 BB = DAG.getTargetLoweringInfo().EmitInstrWithCustomInserter(MI, BB);
Evan Cheng2d373922008-01-30 19:35:32 +0000760 else
761 BB->push_back(MI);
Evan Cheng26639782007-08-02 00:28:15 +0000762
763 // Additional results must be an physical register def.
764 if (HasPhysRegOuts) {
Chris Lattner0c2a4f32008-01-07 03:13:06 +0000765 for (unsigned i = II.getNumDefs(); i < NumResults; ++i) {
766 unsigned Reg = II.getImplicitDefs()[i - II.getNumDefs()];
Evan Cheng0af04f72007-08-02 05:29:38 +0000767 if (Node->hasAnyUseOfValue(i))
Evan Cheng93f143e2007-09-25 01:54:36 +0000768 EmitCopyFromReg(Node, i, InstanceNo, Reg, VRBaseMap);
Evan Cheng26639782007-08-02 00:28:15 +0000769 }
770 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000771 } else {
772 switch (Node->getOpcode()) {
773 default:
774#ifndef NDEBUG
775 Node->dump(&DAG);
776#endif
777 assert(0 && "This target-independent node should have been selected!");
778 case ISD::EntryToken: // fall thru
779 case ISD::TokenFactor:
780 case ISD::LABEL:
Dan Gohmanf14b4472008-01-31 00:25:39 +0000781 case ISD::SRCVALUE:
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000782 break;
783 case ISD::CopyToReg: {
784 unsigned InReg;
785 if (RegisterSDNode *R = dyn_cast<RegisterSDNode>(Node->getOperand(2)))
786 InReg = R->getReg();
787 else
788 InReg = getVR(Node->getOperand(2), VRBaseMap);
789 unsigned DestReg = cast<RegisterSDNode>(Node->getOperand(1))->getReg();
790 if (InReg != DestReg) {// Coalesced away the copy?
791 const TargetRegisterClass *TRC = 0;
792 // Get the target register class
793 if (MRegisterInfo::isVirtualRegister(InReg))
Chris Lattner1b989192007-12-31 04:13:23 +0000794 TRC = RegInfo.getRegClass(InReg);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000795 else
Evan Cheng5ec4b762007-09-26 21:36:17 +0000796 TRC =
797 MRI->getPhysicalRegisterRegClass(Node->getOperand(2).getValueType(),
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000798 InReg);
Owen Anderson8f2c8932007-12-31 06:32:00 +0000799 TII->copyRegToReg(*BB, BB->end(), DestReg, InReg, TRC, TRC);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000800 }
801 break;
802 }
803 case ISD::CopyFromReg: {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000804 unsigned SrcReg = cast<RegisterSDNode>(Node->getOperand(1))->getReg();
Evan Cheng93f143e2007-09-25 01:54:36 +0000805 EmitCopyFromReg(Node, 0, InstanceNo, SrcReg, VRBaseMap);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000806 break;
807 }
808 case ISD::INLINEASM: {
809 unsigned NumOps = Node->getNumOperands();
810 if (Node->getOperand(NumOps-1).getValueType() == MVT::Flag)
811 --NumOps; // Ignore the flag operand.
812
813 // Create the inline asm machine instruction.
814 MachineInstr *MI =
815 new MachineInstr(BB, TII->get(TargetInstrInfo::INLINEASM));
816
817 // Add the asm string as an external symbol operand.
818 const char *AsmStr =
819 cast<ExternalSymbolSDNode>(Node->getOperand(1))->getSymbol();
Chris Lattner8dfd3122007-12-30 00:51:11 +0000820 MI->addOperand(MachineOperand::CreateES(AsmStr));
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000821
822 // Add all of the operand registers to the instruction.
823 for (unsigned i = 2; i != NumOps;) {
824 unsigned Flags = cast<ConstantSDNode>(Node->getOperand(i))->getValue();
825 unsigned NumVals = Flags >> 3;
826
Chris Lattner8dfd3122007-12-30 00:51:11 +0000827 MI->addOperand(MachineOperand::CreateImm(Flags));
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000828 ++i; // Skip the ID value.
829
830 switch (Flags & 7) {
831 default: assert(0 && "Bad flags!");
832 case 1: // Use of register.
833 for (; NumVals; --NumVals, ++i) {
834 unsigned Reg = cast<RegisterSDNode>(Node->getOperand(i))->getReg();
Chris Lattner63ab1f22007-12-30 00:41:17 +0000835 MI->addOperand(MachineOperand::CreateReg(Reg, false));
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000836 }
837 break;
838 case 2: // Def of register.
839 for (; NumVals; --NumVals, ++i) {
840 unsigned Reg = cast<RegisterSDNode>(Node->getOperand(i))->getReg();
Chris Lattner63ab1f22007-12-30 00:41:17 +0000841 MI->addOperand(MachineOperand::CreateReg(Reg, true));
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000842 }
843 break;
844 case 3: { // Immediate.
Chris Lattner23544c12007-08-25 00:53:07 +0000845 for (; NumVals; --NumVals, ++i) {
846 if (ConstantSDNode *CS =
847 dyn_cast<ConstantSDNode>(Node->getOperand(i))) {
Chris Lattner63ab1f22007-12-30 00:41:17 +0000848 MI->addOperand(MachineOperand::CreateImm(CS->getValue()));
Dale Johannesencfb19e62007-11-05 21:20:28 +0000849 } else if (GlobalAddressSDNode *GA =
850 dyn_cast<GlobalAddressSDNode>(Node->getOperand(i))) {
Chris Lattner8dfd3122007-12-30 00:51:11 +0000851 MI->addOperand(MachineOperand::CreateGA(GA->getGlobal(),
852 GA->getOffset()));
Dale Johannesencfb19e62007-11-05 21:20:28 +0000853 } else {
Chris Lattner8dfd3122007-12-30 00:51:11 +0000854 BasicBlockSDNode *BB =cast<BasicBlockSDNode>(Node->getOperand(i));
855 MI->addOperand(MachineOperand::CreateMBB(BB->getBasicBlock()));
Chris Lattner23544c12007-08-25 00:53:07 +0000856 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000857 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000858 break;
859 }
860 case 4: // Addressing mode.
861 // The addressing mode has been selected, just add all of the
862 // operands to the machine instruction.
863 for (; NumVals; --NumVals, ++i)
864 AddOperand(MI, Node->getOperand(i), 0, 0, VRBaseMap);
865 break;
866 }
867 }
868 break;
869 }
870 }
871 }
872}
873
874void ScheduleDAG::EmitNoop() {
875 TII->insertNoop(*BB, BB->end());
876}
877
Evan Cheng5ec4b762007-09-26 21:36:17 +0000878void ScheduleDAG::EmitCrossRCCopy(SUnit *SU, DenseMap<SUnit*, unsigned> &VRBaseMap) {
879 for (SUnit::const_pred_iterator I = SU->Preds.begin(), E = SU->Preds.end();
880 I != E; ++I) {
881 if (I->isCtrl) continue; // ignore chain preds
882 if (!I->Dep->Node) {
883 // Copy to physical register.
884 DenseMap<SUnit*, unsigned>::iterator VRI = VRBaseMap.find(I->Dep);
885 assert(VRI != VRBaseMap.end() && "Node emitted out of order - late");
886 // Find the destination physical register.
887 unsigned Reg = 0;
888 for (SUnit::const_succ_iterator II = SU->Succs.begin(),
889 EE = SU->Succs.end(); II != EE; ++II) {
890 if (I->Reg) {
891 Reg = I->Reg;
892 break;
893 }
894 }
895 assert(I->Reg && "Unknown physical register!");
Owen Anderson8f2c8932007-12-31 06:32:00 +0000896 TII->copyRegToReg(*BB, BB->end(), Reg, VRI->second,
Evan Cheng5ec4b762007-09-26 21:36:17 +0000897 SU->CopyDstRC, SU->CopySrcRC);
898 } else {
899 // Copy from physical register.
900 assert(I->Reg && "Unknown physical register!");
Chris Lattner1b989192007-12-31 04:13:23 +0000901 unsigned VRBase = RegInfo.createVirtualRegister(SU->CopyDstRC);
Evan Cheng5ec4b762007-09-26 21:36:17 +0000902 bool isNew = VRBaseMap.insert(std::make_pair(SU, VRBase));
903 assert(isNew && "Node emitted out of order - early");
Owen Anderson8f2c8932007-12-31 06:32:00 +0000904 TII->copyRegToReg(*BB, BB->end(), VRBase, I->Reg,
Evan Cheng5ec4b762007-09-26 21:36:17 +0000905 SU->CopyDstRC, SU->CopySrcRC);
906 }
907 break;
908 }
909}
910
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000911/// EmitSchedule - Emit the machine code in scheduled order.
912void ScheduleDAG::EmitSchedule() {
913 // If this is the first basic block in the function, and if it has live ins
914 // that need to be copied into vregs, emit the copies into the top of the
915 // block before emitting the code for the block.
Evan Cheng2d373922008-01-30 19:35:32 +0000916 if (&MF->front() == BB) {
Chris Lattner1b989192007-12-31 04:13:23 +0000917 for (MachineRegisterInfo::livein_iterator LI = RegInfo.livein_begin(),
918 E = RegInfo.livein_end(); LI != E; ++LI)
Evan Chengb3d91cf2007-09-26 06:25:56 +0000919 if (LI->second) {
Chris Lattner1b989192007-12-31 04:13:23 +0000920 const TargetRegisterClass *RC = RegInfo.getRegClass(LI->second);
Evan Cheng2d373922008-01-30 19:35:32 +0000921 TII->copyRegToReg(*MF->begin(), MF->begin()->end(), LI->second,
Evan Chengb3d91cf2007-09-26 06:25:56 +0000922 LI->first, RC, RC);
923 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000924 }
925
926
927 // Finally, emit the code for all of the scheduled instructions.
928 DenseMap<SDOperand, unsigned> VRBaseMap;
Evan Cheng5ec4b762007-09-26 21:36:17 +0000929 DenseMap<SUnit*, unsigned> CopyVRBaseMap;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000930 for (unsigned i = 0, e = Sequence.size(); i != e; i++) {
931 if (SUnit *SU = Sequence[i]) {
Evan Cheng93f143e2007-09-25 01:54:36 +0000932 for (unsigned j = 0, ee = SU->FlaggedNodes.size(); j != ee; ++j)
933 EmitNode(SU->FlaggedNodes[j], SU->InstanceNo, VRBaseMap);
Evan Cheng5ec4b762007-09-26 21:36:17 +0000934 if (SU->Node)
935 EmitNode(SU->Node, SU->InstanceNo, VRBaseMap);
936 else
937 EmitCrossRCCopy(SU, CopyVRBaseMap);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000938 } else {
939 // Null SUnit* is a noop.
940 EmitNoop();
941 }
942 }
943}
944
945/// dump - dump the schedule.
946void ScheduleDAG::dumpSchedule() const {
947 for (unsigned i = 0, e = Sequence.size(); i != e; i++) {
948 if (SUnit *SU = Sequence[i])
949 SU->dump(&DAG);
950 else
951 cerr << "**** NOOP ****\n";
952 }
953}
954
955
956/// Run - perform scheduling.
957///
958MachineBasicBlock *ScheduleDAG::Run() {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000959 Schedule();
960 return BB;
961}
962
963/// SUnit - Scheduling unit. It's an wrapper around either a single SDNode or
964/// a group of nodes flagged together.
965void SUnit::dump(const SelectionDAG *G) const {
966 cerr << "SU(" << NodeNum << "): ";
Evan Cheng5ec4b762007-09-26 21:36:17 +0000967 if (Node)
968 Node->dump(G);
969 else
970 cerr << "CROSS RC COPY ";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000971 cerr << "\n";
972 if (FlaggedNodes.size() != 0) {
973 for (unsigned i = 0, e = FlaggedNodes.size(); i != e; i++) {
974 cerr << " ";
975 FlaggedNodes[i]->dump(G);
976 cerr << "\n";
977 }
978 }
979}
980
981void SUnit::dumpAll(const SelectionDAG *G) const {
982 dump(G);
983
984 cerr << " # preds left : " << NumPredsLeft << "\n";
985 cerr << " # succs left : " << NumSuccsLeft << "\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000986 cerr << " Latency : " << Latency << "\n";
987 cerr << " Depth : " << Depth << "\n";
988 cerr << " Height : " << Height << "\n";
989
990 if (Preds.size() != 0) {
991 cerr << " Predecessors:\n";
992 for (SUnit::const_succ_iterator I = Preds.begin(), E = Preds.end();
993 I != E; ++I) {
Evan Chenge7959472007-09-19 01:38:40 +0000994 if (I->isCtrl)
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000995 cerr << " ch #";
996 else
997 cerr << " val #";
Evan Cheng93f143e2007-09-25 01:54:36 +0000998 cerr << I->Dep << " - SU(" << I->Dep->NodeNum << ")";
999 if (I->isSpecial)
1000 cerr << " *";
1001 cerr << "\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001002 }
1003 }
1004 if (Succs.size() != 0) {
1005 cerr << " Successors:\n";
1006 for (SUnit::const_succ_iterator I = Succs.begin(), E = Succs.end();
1007 I != E; ++I) {
Evan Chenge7959472007-09-19 01:38:40 +00001008 if (I->isCtrl)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001009 cerr << " ch #";
1010 else
1011 cerr << " val #";
Evan Cheng93f143e2007-09-25 01:54:36 +00001012 cerr << I->Dep << " - SU(" << I->Dep->NodeNum << ")";
1013 if (I->isSpecial)
1014 cerr << " *";
1015 cerr << "\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001016 }
1017 }
1018 cerr << "\n";
1019}