blob: 250298e2d6095e5430747d11a7d5f67cd1fb2b52 [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();
34 MRI = TM.getRegisterInfo();
35 ConstPool = BB->getParent()->getConstantPool();
36}
Evan Cheng93f143e2007-09-25 01:54:36 +000037
Evan Cheng93f143e2007-09-25 01:54:36 +000038/// CheckForPhysRegDependency - Check if the dependency between def and use of
39/// a specified operand is a physical register dependency. If so, returns the
40/// register and the cost of copying the register.
41static void CheckForPhysRegDependency(SDNode *Def, SDNode *Use, unsigned Op,
42 const MRegisterInfo *MRI,
43 const TargetInstrInfo *TII,
44 unsigned &PhysReg, int &Cost) {
45 if (Op != 2 || Use->getOpcode() != ISD::CopyToReg)
46 return;
47
48 unsigned Reg = cast<RegisterSDNode>(Use->getOperand(1))->getReg();
49 if (MRegisterInfo::isVirtualRegister(Reg))
50 return;
51
52 unsigned ResNo = Use->getOperand(2).ResNo;
53 if (Def->isTargetOpcode()) {
Chris Lattner5b930372008-01-07 07:27:27 +000054 const TargetInstrDesc &II = TII->get(Def->getTargetOpcode());
Chris Lattner0c2a4f32008-01-07 03:13:06 +000055 if (ResNo >= II.getNumDefs() &&
56 II.ImplicitDefs[ResNo - II.getNumDefs()] == Reg) {
Evan Cheng93f143e2007-09-25 01:54:36 +000057 PhysReg = Reg;
58 const TargetRegisterClass *RC =
Evan Cheng5ec4b762007-09-26 21:36:17 +000059 MRI->getPhysicalRegisterRegClass(Def->getValueType(ResNo), Reg);
Evan Cheng93f143e2007-09-25 01:54:36 +000060 Cost = RC->getCopyCost();
61 }
62 }
63}
64
65SUnit *ScheduleDAG::Clone(SUnit *Old) {
66 SUnit *SU = NewSUnit(Old->Node);
67 for (unsigned i = 0, e = SU->FlaggedNodes.size(); i != e; ++i)
68 SU->FlaggedNodes.push_back(SU->FlaggedNodes[i]);
69 SU->InstanceNo = SUnitMap[Old->Node].size();
70 SU->Latency = Old->Latency;
71 SU->isTwoAddress = Old->isTwoAddress;
72 SU->isCommutable = Old->isCommutable;
Evan Chengba597da2007-09-28 22:32:30 +000073 SU->hasPhysRegDefs = Old->hasPhysRegDefs;
Evan Cheng93f143e2007-09-25 01:54:36 +000074 SUnitMap[Old->Node].push_back(SU);
75 return SU;
76}
77
Evan Chengdd3f8b92007-10-05 01:39:18 +000078
Dan Gohmanf17a25c2007-07-18 16:29:46 +000079/// BuildSchedUnits - Build SUnits from the selection dag that we are input.
80/// This SUnit graph is similar to the SelectionDAG, but represents flagged
81/// together nodes with a single SUnit.
82void ScheduleDAG::BuildSchedUnits() {
83 // Reserve entries in the vector for each of the SUnits we are creating. This
84 // ensure that reallocation of the vector won't happen, so SUnit*'s won't get
85 // invalidated.
86 SUnits.reserve(std::distance(DAG.allnodes_begin(), DAG.allnodes_end()));
87
Dan Gohmanf17a25c2007-07-18 16:29:46 +000088 for (SelectionDAG::allnodes_iterator NI = DAG.allnodes_begin(),
89 E = DAG.allnodes_end(); NI != E; ++NI) {
90 if (isPassiveNode(NI)) // Leaf node, e.g. a TargetImmediate.
91 continue;
92
93 // If this node has already been processed, stop now.
Evan Cheng93f143e2007-09-25 01:54:36 +000094 if (SUnitMap[NI].size()) continue;
Dan Gohmanf17a25c2007-07-18 16:29:46 +000095
96 SUnit *NodeSUnit = NewSUnit(NI);
97
98 // See if anything is flagged to this node, if so, add them to flagged
99 // nodes. Nodes can have at most one flag input and one flag output. Flags
100 // are required the be the last operand and result of a node.
101
102 // Scan up, adding flagged preds to FlaggedNodes.
103 SDNode *N = NI;
104 if (N->getNumOperands() &&
105 N->getOperand(N->getNumOperands()-1).getValueType() == MVT::Flag) {
106 do {
107 N = N->getOperand(N->getNumOperands()-1).Val;
108 NodeSUnit->FlaggedNodes.push_back(N);
Evan Cheng93f143e2007-09-25 01:54:36 +0000109 SUnitMap[N].push_back(NodeSUnit);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000110 } while (N->getNumOperands() &&
111 N->getOperand(N->getNumOperands()-1).getValueType()== MVT::Flag);
112 std::reverse(NodeSUnit->FlaggedNodes.begin(),
113 NodeSUnit->FlaggedNodes.end());
114 }
115
116 // Scan down, adding this node and any flagged succs to FlaggedNodes if they
117 // have a user of the flag operand.
118 N = NI;
119 while (N->getValueType(N->getNumValues()-1) == MVT::Flag) {
120 SDOperand FlagVal(N, N->getNumValues()-1);
121
122 // There are either zero or one users of the Flag result.
123 bool HasFlagUse = false;
124 for (SDNode::use_iterator UI = N->use_begin(), E = N->use_end();
125 UI != E; ++UI)
126 if (FlagVal.isOperand(*UI)) {
127 HasFlagUse = true;
128 NodeSUnit->FlaggedNodes.push_back(N);
Evan Cheng93f143e2007-09-25 01:54:36 +0000129 SUnitMap[N].push_back(NodeSUnit);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000130 N = *UI;
131 break;
132 }
133 if (!HasFlagUse) break;
134 }
135
136 // Now all flagged nodes are in FlaggedNodes and N is the bottom-most node.
137 // Update the SUnit
138 NodeSUnit->Node = N;
Evan Cheng93f143e2007-09-25 01:54:36 +0000139 SUnitMap[N].push_back(NodeSUnit);
Evan Chengdd3f8b92007-10-05 01:39:18 +0000140
141 ComputeLatency(NodeSUnit);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000142 }
143
144 // Pass 2: add the preds, succs, etc.
145 for (unsigned su = 0, e = SUnits.size(); su != e; ++su) {
146 SUnit *SU = &SUnits[su];
147 SDNode *MainNode = SU->Node;
148
149 if (MainNode->isTargetOpcode()) {
150 unsigned Opc = MainNode->getTargetOpcode();
Chris Lattner5b930372008-01-07 07:27:27 +0000151 const TargetInstrDesc &TID = TII->get(Opc);
Chris Lattner0c2a4f32008-01-07 03:13:06 +0000152 for (unsigned i = 0; i != TID.getNumOperands(); ++i) {
Evan Cheng93f143e2007-09-25 01:54:36 +0000153 if (TID.getOperandConstraint(i, TOI::TIED_TO) != -1) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000154 SU->isTwoAddress = true;
155 break;
156 }
157 }
Chris Lattnerd8529ab2008-01-07 06:42:05 +0000158 if (TID.isCommutable())
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000159 SU->isCommutable = true;
160 }
161
162 // Find all predecessors and successors of the group.
163 // Temporarily add N to make code simpler.
164 SU->FlaggedNodes.push_back(MainNode);
165
166 for (unsigned n = 0, e = SU->FlaggedNodes.size(); n != e; ++n) {
167 SDNode *N = SU->FlaggedNodes[n];
Evan Chengba597da2007-09-28 22:32:30 +0000168 if (N->isTargetOpcode() &&
Chris Lattner0c2a4f32008-01-07 03:13:06 +0000169 TII->get(N->getTargetOpcode()).getImplicitDefs() &&
170 CountResults(N) > TII->get(N->getTargetOpcode()).getNumDefs())
Evan Chengba597da2007-09-28 22:32:30 +0000171 SU->hasPhysRegDefs = true;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000172
173 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
174 SDNode *OpN = N->getOperand(i).Val;
175 if (isPassiveNode(OpN)) continue; // Not scheduled.
Evan Cheng93f143e2007-09-25 01:54:36 +0000176 SUnit *OpSU = SUnitMap[OpN].front();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000177 assert(OpSU && "Node has no SUnit!");
178 if (OpSU == SU) continue; // In the same group.
179
180 MVT::ValueType OpVT = N->getOperand(i).getValueType();
181 assert(OpVT != MVT::Flag && "Flagged nodes should be in same sunit!");
182 bool isChain = OpVT == MVT::Other;
Evan Cheng93f143e2007-09-25 01:54:36 +0000183
184 unsigned PhysReg = 0;
185 int Cost = 1;
186 // Determine if this is a physical register dependency.
187 CheckForPhysRegDependency(OpN, N, i, MRI, TII, PhysReg, Cost);
188 SU->addPred(OpSU, isChain, false, PhysReg, Cost);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000189 }
190 }
191
192 // Remove MainNode from FlaggedNodes again.
193 SU->FlaggedNodes.pop_back();
194 }
195
196 return;
197}
198
Evan Chengdd3f8b92007-10-05 01:39:18 +0000199void ScheduleDAG::ComputeLatency(SUnit *SU) {
200 const InstrItineraryData &InstrItins = TM.getInstrItineraryData();
201
202 // Compute the latency for the node. We use the sum of the latencies for
203 // all nodes flagged together into this SUnit.
204 if (InstrItins.isEmpty()) {
205 // No latency information.
206 SU->Latency = 1;
207 } else {
208 SU->Latency = 0;
209 if (SU->Node->isTargetOpcode()) {
Chris Lattner3d54fcd2008-01-07 02:46:03 +0000210 unsigned SchedClass =
211 TII->get(SU->Node->getTargetOpcode()).getSchedClass();
Evan Chengdd3f8b92007-10-05 01:39:18 +0000212 InstrStage *S = InstrItins.begin(SchedClass);
213 InstrStage *E = InstrItins.end(SchedClass);
214 for (; S != E; ++S)
215 SU->Latency += S->Cycles;
216 }
217 for (unsigned i = 0, e = SU->FlaggedNodes.size(); i != e; ++i) {
218 SDNode *FNode = SU->FlaggedNodes[i];
219 if (FNode->isTargetOpcode()) {
Chris Lattner3d54fcd2008-01-07 02:46:03 +0000220 unsigned SchedClass =TII->get(FNode->getTargetOpcode()).getSchedClass();
Evan Chengdd3f8b92007-10-05 01:39:18 +0000221 InstrStage *S = InstrItins.begin(SchedClass);
222 InstrStage *E = InstrItins.end(SchedClass);
223 for (; S != E; ++S)
224 SU->Latency += S->Cycles;
225 }
226 }
227 }
228}
229
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000230void ScheduleDAG::CalculateDepths() {
231 std::vector<std::pair<SUnit*, unsigned> > WorkList;
232 for (unsigned i = 0, e = SUnits.size(); i != e; ++i)
Evan Cheng96689602007-09-12 23:45:46 +0000233 if (SUnits[i].Preds.size() == 0)
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000234 WorkList.push_back(std::make_pair(&SUnits[i], 0U));
235
236 while (!WorkList.empty()) {
237 SUnit *SU = WorkList.back().first;
238 unsigned Depth = WorkList.back().second;
239 WorkList.pop_back();
240 if (SU->Depth == 0 || Depth > SU->Depth) {
241 SU->Depth = Depth;
242 for (SUnit::succ_iterator I = SU->Succs.begin(), E = SU->Succs.end();
243 I != E; ++I)
Evan Chenge7959472007-09-19 01:38:40 +0000244 WorkList.push_back(std::make_pair(I->Dep, Depth+1));
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000245 }
246 }
247}
248
249void ScheduleDAG::CalculateHeights() {
250 std::vector<std::pair<SUnit*, unsigned> > WorkList;
Evan Cheng93f143e2007-09-25 01:54:36 +0000251 SUnit *Root = SUnitMap[DAG.getRoot().Val].front();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000252 WorkList.push_back(std::make_pair(Root, 0U));
253
254 while (!WorkList.empty()) {
255 SUnit *SU = WorkList.back().first;
256 unsigned Height = WorkList.back().second;
257 WorkList.pop_back();
258 if (SU->Height == 0 || Height > SU->Height) {
259 SU->Height = Height;
260 for (SUnit::pred_iterator I = SU->Preds.begin(), E = SU->Preds.end();
261 I != E; ++I)
Evan Chenge7959472007-09-19 01:38:40 +0000262 WorkList.push_back(std::make_pair(I->Dep, Height+1));
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000263 }
264 }
265}
266
267/// CountResults - The results of target nodes have register or immediate
268/// operands first, then an optional chain, and optional flag operands (which do
269/// not go into the machine instrs.)
270unsigned ScheduleDAG::CountResults(SDNode *Node) {
271 unsigned N = Node->getNumValues();
272 while (N && Node->getValueType(N - 1) == MVT::Flag)
273 --N;
274 if (N && Node->getValueType(N - 1) == MVT::Other)
275 --N; // Skip over chain result.
276 return N;
277}
278
279/// CountOperands The inputs to target nodes have any actual inputs first,
280/// followed by an optional chain operand, then flag operands. Compute the
281/// number of actual operands that will go into the machine instr.
282unsigned ScheduleDAG::CountOperands(SDNode *Node) {
283 unsigned N = Node->getNumOperands();
284 while (N && Node->getOperand(N - 1).getValueType() == MVT::Flag)
285 --N;
286 if (N && Node->getOperand(N - 1).getValueType() == MVT::Other)
287 --N; // Ignore chain if it exists.
288 return N;
289}
290
291static const TargetRegisterClass *getInstrOperandRegClass(
292 const MRegisterInfo *MRI,
293 const TargetInstrInfo *TII,
Chris Lattner5b930372008-01-07 07:27:27 +0000294 const TargetInstrDesc &II,
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000295 unsigned Op) {
Chris Lattner5b930372008-01-07 07:27:27 +0000296 if (Op >= II.getNumOperands()) {
297 assert(II.isVariadic() && "Invalid operand # of instruction");
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000298 return NULL;
299 }
Chris Lattner5b930372008-01-07 07:27:27 +0000300 if (II.OpInfo[Op].isLookupPtrRegClass())
Chris Lattnereeedb482008-01-07 02:39:19 +0000301 return TII->getPointerRegClass();
Chris Lattner5b930372008-01-07 07:27:27 +0000302 return MRI->getRegClass(II.OpInfo[Op].RegClass);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000303}
304
Evan Cheng93f143e2007-09-25 01:54:36 +0000305void ScheduleDAG::EmitCopyFromReg(SDNode *Node, unsigned ResNo,
306 unsigned InstanceNo, unsigned SrcReg,
Evan Cheng26639782007-08-02 00:28:15 +0000307 DenseMap<SDOperand, unsigned> &VRBaseMap) {
308 unsigned VRBase = 0;
309 if (MRegisterInfo::isVirtualRegister(SrcReg)) {
310 // Just use the input register directly!
Evan Cheng93f143e2007-09-25 01:54:36 +0000311 if (InstanceNo > 0)
312 VRBaseMap.erase(SDOperand(Node, ResNo));
Evan Cheng26639782007-08-02 00:28:15 +0000313 bool isNew = VRBaseMap.insert(std::make_pair(SDOperand(Node,ResNo),SrcReg));
314 assert(isNew && "Node emitted out of order - early");
315 return;
316 }
317
318 // If the node is only used by a CopyToReg and the dest reg is a vreg, use
319 // the CopyToReg'd destination register instead of creating a new vreg.
Evan Cheng93f143e2007-09-25 01:54:36 +0000320 bool MatchReg = true;
Evan Cheng26639782007-08-02 00:28:15 +0000321 for (SDNode::use_iterator UI = Node->use_begin(), E = Node->use_end();
322 UI != E; ++UI) {
323 SDNode *Use = *UI;
Evan Cheng93f143e2007-09-25 01:54:36 +0000324 bool Match = true;
Evan Cheng26639782007-08-02 00:28:15 +0000325 if (Use->getOpcode() == ISD::CopyToReg &&
326 Use->getOperand(2).Val == Node &&
327 Use->getOperand(2).ResNo == ResNo) {
328 unsigned DestReg = cast<RegisterSDNode>(Use->getOperand(1))->getReg();
329 if (MRegisterInfo::isVirtualRegister(DestReg)) {
330 VRBase = DestReg;
Evan Cheng93f143e2007-09-25 01:54:36 +0000331 Match = false;
332 } else if (DestReg != SrcReg)
333 Match = false;
334 } else {
335 for (unsigned i = 0, e = Use->getNumOperands(); i != e; ++i) {
336 SDOperand Op = Use->getOperand(i);
Evan Cheng4f0345c2007-12-14 08:25:15 +0000337 if (Op.Val != Node || Op.ResNo != ResNo)
Evan Cheng93f143e2007-09-25 01:54:36 +0000338 continue;
339 MVT::ValueType VT = Node->getValueType(Op.ResNo);
340 if (VT != MVT::Other && VT != MVT::Flag)
341 Match = false;
Evan Cheng26639782007-08-02 00:28:15 +0000342 }
343 }
Evan Cheng93f143e2007-09-25 01:54:36 +0000344 MatchReg &= Match;
345 if (VRBase)
346 break;
Evan Cheng26639782007-08-02 00:28:15 +0000347 }
348
Evan Cheng26639782007-08-02 00:28:15 +0000349 const TargetRegisterClass *TRC = 0;
Evan Cheng93f143e2007-09-25 01:54:36 +0000350 // Figure out the register class to create for the destreg.
351 if (VRBase)
Chris Lattner1b989192007-12-31 04:13:23 +0000352 TRC = RegInfo.getRegClass(VRBase);
Evan Cheng93f143e2007-09-25 01:54:36 +0000353 else
Evan Cheng5ec4b762007-09-26 21:36:17 +0000354 TRC = MRI->getPhysicalRegisterRegClass(Node->getValueType(ResNo), SrcReg);
Evan Cheng93f143e2007-09-25 01:54:36 +0000355
356 // If all uses are reading from the src physical register and copying the
357 // register is either impossible or very expensive, then don't create a copy.
358 if (MatchReg && TRC->getCopyCost() < 0) {
359 VRBase = SrcReg;
360 } else {
Evan Cheng26639782007-08-02 00:28:15 +0000361 // Create the reg, emit the copy.
Chris Lattner1b989192007-12-31 04:13:23 +0000362 VRBase = RegInfo.createVirtualRegister(TRC);
Owen Anderson8f2c8932007-12-31 06:32:00 +0000363 TII->copyRegToReg(*BB, BB->end(), VRBase, SrcReg, TRC, TRC);
Evan Cheng26639782007-08-02 00:28:15 +0000364 }
Evan Cheng26639782007-08-02 00:28:15 +0000365
Evan Cheng93f143e2007-09-25 01:54:36 +0000366 if (InstanceNo > 0)
367 VRBaseMap.erase(SDOperand(Node, ResNo));
Evan Cheng26639782007-08-02 00:28:15 +0000368 bool isNew = VRBaseMap.insert(std::make_pair(SDOperand(Node,ResNo), VRBase));
369 assert(isNew && "Node emitted out of order - early");
370}
371
372void ScheduleDAG::CreateVirtualRegisters(SDNode *Node,
373 MachineInstr *MI,
Chris Lattner5b930372008-01-07 07:27:27 +0000374 const TargetInstrDesc &II,
Evan Cheng26639782007-08-02 00:28:15 +0000375 DenseMap<SDOperand, unsigned> &VRBaseMap) {
Chris Lattner0c2a4f32008-01-07 03:13:06 +0000376 for (unsigned i = 0; i < II.getNumDefs(); ++i) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000377 // If the specific node value is only used by a CopyToReg and the dest reg
378 // is a vreg, use the CopyToReg'd destination register instead of creating
379 // a new vreg.
380 unsigned VRBase = 0;
381 for (SDNode::use_iterator UI = Node->use_begin(), E = Node->use_end();
382 UI != E; ++UI) {
383 SDNode *Use = *UI;
384 if (Use->getOpcode() == ISD::CopyToReg &&
385 Use->getOperand(2).Val == Node &&
386 Use->getOperand(2).ResNo == i) {
387 unsigned Reg = cast<RegisterSDNode>(Use->getOperand(1))->getReg();
388 if (MRegisterInfo::isVirtualRegister(Reg)) {
389 VRBase = Reg;
Chris Lattner63ab1f22007-12-30 00:41:17 +0000390 MI->addOperand(MachineOperand::CreateReg(Reg, true));
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000391 break;
392 }
393 }
394 }
395
Evan Cheng26639782007-08-02 00:28:15 +0000396 // Create the result registers for this node and add the result regs to
397 // the machine instruction.
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000398 if (VRBase == 0) {
Chris Lattner5b930372008-01-07 07:27:27 +0000399 const TargetRegisterClass *RC = getInstrOperandRegClass(MRI, TII, II, i);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000400 assert(RC && "Isn't a register operand!");
Chris Lattner1b989192007-12-31 04:13:23 +0000401 VRBase = RegInfo.createVirtualRegister(RC);
Chris Lattner63ab1f22007-12-30 00:41:17 +0000402 MI->addOperand(MachineOperand::CreateReg(VRBase, true));
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000403 }
404
405 bool isNew = VRBaseMap.insert(std::make_pair(SDOperand(Node,i), VRBase));
406 assert(isNew && "Node emitted out of order - early");
407 }
408}
409
410/// getVR - Return the virtual register corresponding to the specified result
411/// of the specified node.
412static unsigned getVR(SDOperand Op, DenseMap<SDOperand, unsigned> &VRBaseMap) {
413 DenseMap<SDOperand, unsigned>::iterator I = VRBaseMap.find(Op);
414 assert(I != VRBaseMap.end() && "Node emitted out of order - late");
415 return I->second;
416}
417
418
419/// AddOperand - Add the specified operand to the specified machine instr. II
420/// specifies the instruction information for the node, and IIOpNum is the
421/// operand number (in the II) that we are adding. IIOpNum and II are used for
422/// assertions only.
423void ScheduleDAG::AddOperand(MachineInstr *MI, SDOperand Op,
424 unsigned IIOpNum,
Chris Lattner5b930372008-01-07 07:27:27 +0000425 const TargetInstrDesc *II,
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000426 DenseMap<SDOperand, unsigned> &VRBaseMap) {
427 if (Op.isTargetOpcode()) {
428 // Note that this case is redundant with the final else block, but we
429 // include it because it is the most common and it makes the logic
430 // simpler here.
431 assert(Op.getValueType() != MVT::Other &&
432 Op.getValueType() != MVT::Flag &&
433 "Chain and flag operands should occur at end of operand list!");
434
435 // Get/emit the operand.
436 unsigned VReg = getVR(Op, VRBaseMap);
Chris Lattner5b930372008-01-07 07:27:27 +0000437 const TargetInstrDesc &TID = MI->getDesc();
438 bool isOptDef = (IIOpNum < TID.getNumOperands())
439 ? (TID.OpInfo[IIOpNum].isOptionalDef()) : false;
Chris Lattner63ab1f22007-12-30 00:41:17 +0000440 MI->addOperand(MachineOperand::CreateReg(VReg, isOptDef));
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000441
442 // Verify that it is right.
443 assert(MRegisterInfo::isVirtualRegister(VReg) && "Not a vreg?");
444 if (II) {
445 const TargetRegisterClass *RC =
Chris Lattner5b930372008-01-07 07:27:27 +0000446 getInstrOperandRegClass(MRI, TII, *II, IIOpNum);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000447 assert(RC && "Don't have operand info for this instruction!");
Chris Lattner1b989192007-12-31 04:13:23 +0000448 const TargetRegisterClass *VRC = RegInfo.getRegClass(VReg);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000449 if (VRC != RC) {
450 cerr << "Register class of operand and regclass of use don't agree!\n";
451#ifndef NDEBUG
452 cerr << "Operand = " << IIOpNum << "\n";
453 cerr << "Op->Val = "; Op.Val->dump(&DAG); cerr << "\n";
454 cerr << "MI = "; MI->print(cerr);
455 cerr << "VReg = " << VReg << "\n";
456 cerr << "VReg RegClass size = " << VRC->getSize()
457 << ", align = " << VRC->getAlignment() << "\n";
458 cerr << "Expected RegClass size = " << RC->getSize()
459 << ", align = " << RC->getAlignment() << "\n";
460#endif
461 cerr << "Fatal error, aborting.\n";
462 abort();
463 }
464 }
Chris Lattner8dfd3122007-12-30 00:51:11 +0000465 } else if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Chris Lattner63ab1f22007-12-30 00:41:17 +0000466 MI->addOperand(MachineOperand::CreateImm(C->getValue()));
Chris Lattner8dfd3122007-12-30 00:51:11 +0000467 } else if (RegisterSDNode *R = dyn_cast<RegisterSDNode>(Op)) {
Chris Lattner63ab1f22007-12-30 00:41:17 +0000468 MI->addOperand(MachineOperand::CreateReg(R->getReg(), false));
Chris Lattner8dfd3122007-12-30 00:51:11 +0000469 } else if (GlobalAddressSDNode *TGA = dyn_cast<GlobalAddressSDNode>(Op)) {
470 MI->addOperand(MachineOperand::CreateGA(TGA->getGlobal(),TGA->getOffset()));
471 } else if (BasicBlockSDNode *BB = dyn_cast<BasicBlockSDNode>(Op)) {
472 MI->addOperand(MachineOperand::CreateMBB(BB->getBasicBlock()));
473 } else if (FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(Op)) {
474 MI->addOperand(MachineOperand::CreateFI(FI->getIndex()));
475 } else if (JumpTableSDNode *JT = dyn_cast<JumpTableSDNode>(Op)) {
476 MI->addOperand(MachineOperand::CreateJTI(JT->getIndex()));
477 } else if (ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(Op)) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000478 int Offset = CP->getOffset();
479 unsigned Align = CP->getAlignment();
480 const Type *Type = CP->getType();
481 // MachineConstantPool wants an explicit alignment.
482 if (Align == 0) {
483 Align = TM.getTargetData()->getPreferredTypeAlignmentShift(Type);
484 if (Align == 0) {
485 // Alignment of vector types. FIXME!
Duncan Sandsf99fdc62007-11-01 20:53:16 +0000486 Align = TM.getTargetData()->getABITypeSize(Type);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000487 Align = Log2_64(Align);
488 }
489 }
490
491 unsigned Idx;
492 if (CP->isMachineConstantPoolEntry())
493 Idx = ConstPool->getConstantPoolIndex(CP->getMachineCPVal(), Align);
494 else
495 Idx = ConstPool->getConstantPoolIndex(CP->getConstVal(), Align);
Chris Lattner8dfd3122007-12-30 00:51:11 +0000496 MI->addOperand(MachineOperand::CreateCPI(Idx, Offset));
497 } else if (ExternalSymbolSDNode *ES = dyn_cast<ExternalSymbolSDNode>(Op)) {
498 MI->addOperand(MachineOperand::CreateES(ES->getSymbol()));
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000499 } else {
500 assert(Op.getValueType() != MVT::Other &&
501 Op.getValueType() != MVT::Flag &&
502 "Chain and flag operands should occur at end of operand list!");
503 unsigned VReg = getVR(Op, VRBaseMap);
Chris Lattner63ab1f22007-12-30 00:41:17 +0000504 MI->addOperand(MachineOperand::CreateReg(VReg, false));
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000505
506 // Verify that it is right.
507 assert(MRegisterInfo::isVirtualRegister(VReg) && "Not a vreg?");
508 if (II) {
509 const TargetRegisterClass *RC =
Chris Lattner5b930372008-01-07 07:27:27 +0000510 getInstrOperandRegClass(MRI, TII, *II, IIOpNum);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000511 assert(RC && "Don't have operand info for this instruction!");
Chris Lattner1b989192007-12-31 04:13:23 +0000512 assert(RegInfo.getRegClass(VReg) == RC &&
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000513 "Register class of operand and regclass of use don't agree!");
514 }
515 }
516
517}
518
Christopher Lambe95328d2007-07-26 08:12:07 +0000519// Returns the Register Class of a subregister
520static const TargetRegisterClass *getSubRegisterRegClass(
521 const TargetRegisterClass *TRC,
522 unsigned SubIdx) {
523 // Pick the register class of the subregister
524 MRegisterInfo::regclass_iterator I = TRC->subregclasses_begin() + SubIdx-1;
525 assert(I < TRC->subregclasses_end() &&
526 "Invalid subregister index for register class");
527 return *I;
528}
529
530static const TargetRegisterClass *getSuperregRegisterClass(
531 const TargetRegisterClass *TRC,
532 unsigned SubIdx,
533 MVT::ValueType VT) {
534 // Pick the register class of the superegister for this type
535 for (MRegisterInfo::regclass_iterator I = TRC->superregclasses_begin(),
536 E = TRC->superregclasses_end(); I != E; ++I)
537 if ((*I)->hasType(VT) && getSubRegisterRegClass(*I, SubIdx) == TRC)
538 return *I;
539 assert(false && "Couldn't find the register class");
540 return 0;
541}
542
543/// EmitSubregNode - Generate machine code for subreg nodes.
544///
545void ScheduleDAG::EmitSubregNode(SDNode *Node,
546 DenseMap<SDOperand, unsigned> &VRBaseMap) {
547 unsigned VRBase = 0;
548 unsigned Opc = Node->getTargetOpcode();
549 if (Opc == TargetInstrInfo::EXTRACT_SUBREG) {
550 // If the node is only used by a CopyToReg and the dest reg is a vreg, use
551 // the CopyToReg'd destination register instead of creating a new vreg.
552 for (SDNode::use_iterator UI = Node->use_begin(), E = Node->use_end();
553 UI != E; ++UI) {
554 SDNode *Use = *UI;
555 if (Use->getOpcode() == ISD::CopyToReg &&
556 Use->getOperand(2).Val == Node) {
557 unsigned DestReg = cast<RegisterSDNode>(Use->getOperand(1))->getReg();
558 if (MRegisterInfo::isVirtualRegister(DestReg)) {
559 VRBase = DestReg;
560 break;
561 }
562 }
563 }
564
565 unsigned SubIdx = cast<ConstantSDNode>(Node->getOperand(1))->getValue();
566
567 // TODO: If the node is a use of a CopyFromReg from a physical register
568 // fold the extract into the copy now
569
Christopher Lambe95328d2007-07-26 08:12:07 +0000570 // Create the extract_subreg machine instruction.
571 MachineInstr *MI =
572 new MachineInstr(BB, TII->get(TargetInstrInfo::EXTRACT_SUBREG));
573
574 // Figure out the register class to create for the destreg.
575 unsigned VReg = getVR(Node->getOperand(0), VRBaseMap);
Chris Lattner1b989192007-12-31 04:13:23 +0000576 const TargetRegisterClass *TRC = RegInfo.getRegClass(VReg);
Christopher Lambe95328d2007-07-26 08:12:07 +0000577 const TargetRegisterClass *SRC = getSubRegisterRegClass(TRC, SubIdx);
578
579 if (VRBase) {
580 // Grab the destination register
581 const TargetRegisterClass *DRC = 0;
Chris Lattner1b989192007-12-31 04:13:23 +0000582 DRC = RegInfo.getRegClass(VRBase);
Christopher Lambe95328d2007-07-26 08:12:07 +0000583 assert(SRC == DRC &&
584 "Source subregister and destination must have the same class");
585 } else {
586 // Create the reg
Chris Lattner1b989192007-12-31 04:13:23 +0000587 VRBase = RegInfo.createVirtualRegister(SRC);
Christopher Lambe95328d2007-07-26 08:12:07 +0000588 }
589
590 // Add def, source, and subreg index
Chris Lattner63ab1f22007-12-30 00:41:17 +0000591 MI->addOperand(MachineOperand::CreateReg(VRBase, true));
Christopher Lambe95328d2007-07-26 08:12:07 +0000592 AddOperand(MI, Node->getOperand(0), 0, 0, VRBaseMap);
Chris Lattner8dfd3122007-12-30 00:51:11 +0000593 MI->addOperand(MachineOperand::CreateImm(SubIdx));
Christopher Lambe95328d2007-07-26 08:12:07 +0000594
595 } else if (Opc == TargetInstrInfo::INSERT_SUBREG) {
596 assert((Node->getNumOperands() == 2 || Node->getNumOperands() == 3) &&
597 "Malformed insert_subreg node");
598 bool isUndefInput = (Node->getNumOperands() == 2);
599 unsigned SubReg = 0;
600 unsigned SubIdx = 0;
601
602 if (isUndefInput) {
603 SubReg = getVR(Node->getOperand(0), VRBaseMap);
604 SubIdx = cast<ConstantSDNode>(Node->getOperand(1))->getValue();
605 } else {
606 SubReg = getVR(Node->getOperand(1), VRBaseMap);
607 SubIdx = cast<ConstantSDNode>(Node->getOperand(2))->getValue();
608 }
609
Chris Lattnerb70e1512007-12-31 04:16:08 +0000610 // TODO: Add tracking info to MachineRegisterInfo of which vregs are subregs
Christopher Lambe95328d2007-07-26 08:12:07 +0000611 // to allow coalescing in the allocator
612
613 // If the node is only used by a CopyToReg and the dest reg is a vreg, use
614 // the CopyToReg'd destination register instead of creating a new vreg.
615 // If the CopyToReg'd destination register is physical, then fold the
616 // insert into the copy
617 for (SDNode::use_iterator UI = Node->use_begin(), E = Node->use_end();
618 UI != E; ++UI) {
619 SDNode *Use = *UI;
620 if (Use->getOpcode() == ISD::CopyToReg &&
621 Use->getOperand(2).Val == Node) {
622 unsigned DestReg = cast<RegisterSDNode>(Use->getOperand(1))->getReg();
623 if (MRegisterInfo::isVirtualRegister(DestReg)) {
624 VRBase = DestReg;
625 break;
626 }
627 }
628 }
629
630 // Create the insert_subreg machine instruction.
631 MachineInstr *MI =
632 new MachineInstr(BB, TII->get(TargetInstrInfo::INSERT_SUBREG));
633
634 // Figure out the register class to create for the destreg.
635 const TargetRegisterClass *TRC = 0;
636 if (VRBase) {
Chris Lattner1b989192007-12-31 04:13:23 +0000637 TRC = RegInfo.getRegClass(VRBase);
Christopher Lambe95328d2007-07-26 08:12:07 +0000638 } else {
Chris Lattner1b989192007-12-31 04:13:23 +0000639 TRC = getSuperregRegisterClass(RegInfo.getRegClass(SubReg), SubIdx,
Christopher Lambe95328d2007-07-26 08:12:07 +0000640 Node->getValueType(0));
641 assert(TRC && "Couldn't determine register class for insert_subreg");
Chris Lattner1b989192007-12-31 04:13:23 +0000642 VRBase = RegInfo.createVirtualRegister(TRC); // Create the reg
Christopher Lambe95328d2007-07-26 08:12:07 +0000643 }
644
Chris Lattner63ab1f22007-12-30 00:41:17 +0000645 MI->addOperand(MachineOperand::CreateReg(VRBase, true));
Christopher Lambe95328d2007-07-26 08:12:07 +0000646 AddOperand(MI, Node->getOperand(0), 0, 0, VRBaseMap);
647 if (!isUndefInput)
648 AddOperand(MI, Node->getOperand(1), 0, 0, VRBaseMap);
Chris Lattner8dfd3122007-12-30 00:51:11 +0000649 MI->addOperand(MachineOperand::CreateImm(SubIdx));
Christopher Lambe95328d2007-07-26 08:12:07 +0000650 } else
651 assert(0 && "Node is not a subreg insert or extract");
652
653 bool isNew = VRBaseMap.insert(std::make_pair(SDOperand(Node,0), VRBase));
654 assert(isNew && "Node emitted out of order - early");
655}
656
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000657/// EmitNode - Generate machine code for an node and needed dependencies.
658///
Evan Cheng93f143e2007-09-25 01:54:36 +0000659void ScheduleDAG::EmitNode(SDNode *Node, unsigned InstanceNo,
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000660 DenseMap<SDOperand, unsigned> &VRBaseMap) {
661 // If machine instruction
662 if (Node->isTargetOpcode()) {
663 unsigned Opc = Node->getTargetOpcode();
Christopher Lambe95328d2007-07-26 08:12:07 +0000664
665 // Handle subreg insert/extract specially
666 if (Opc == TargetInstrInfo::EXTRACT_SUBREG ||
667 Opc == TargetInstrInfo::INSERT_SUBREG) {
668 EmitSubregNode(Node, VRBaseMap);
669 return;
670 }
671
Chris Lattner5b930372008-01-07 07:27:27 +0000672 const TargetInstrDesc &II = TII->get(Opc);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000673
674 unsigned NumResults = CountResults(Node);
675 unsigned NodeOperands = CountOperands(Node);
676 unsigned NumMIOperands = NodeOperands + NumResults;
Chris Lattner0c2a4f32008-01-07 03:13:06 +0000677 bool HasPhysRegOuts = (NumResults > II.getNumDefs()) &&
678 II.getImplicitDefs() != 0;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000679#ifndef NDEBUG
Chris Lattner0c2a4f32008-01-07 03:13:06 +0000680 assert((II.getNumOperands() == NumMIOperands ||
Chris Lattner2fb37c02008-01-07 05:19:29 +0000681 HasPhysRegOuts || II.isVariadic()) &&
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000682 "#operands for dag node doesn't match .td file!");
683#endif
684
685 // Create the new machine instruction.
686 MachineInstr *MI = new MachineInstr(II);
687
688 // Add result register values for things that are defined by this
689 // instruction.
690 if (NumResults)
Evan Cheng26639782007-08-02 00:28:15 +0000691 CreateVirtualRegisters(Node, MI, II, VRBaseMap);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000692
693 // Emit all of the actual operands of this instruction, adding them to the
694 // instruction as appropriate.
695 for (unsigned i = 0; i != NodeOperands; ++i)
Chris Lattner0c2a4f32008-01-07 03:13:06 +0000696 AddOperand(MI, Node->getOperand(i), i+II.getNumDefs(), &II, VRBaseMap);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000697
698 // Commute node if it has been determined to be profitable.
699 if (CommuteSet.count(Node)) {
700 MachineInstr *NewMI = TII->commuteInstruction(MI);
701 if (NewMI == 0)
702 DOUT << "Sched: COMMUTING FAILED!\n";
703 else {
704 DOUT << "Sched: COMMUTED TO: " << *NewMI;
705 if (MI != NewMI) {
706 delete MI;
707 MI = NewMI;
708 }
709 }
710 }
711
712 // Now that we have emitted all operands, emit this instruction itself.
Chris Lattnerd6bfd8a2008-01-07 06:21:53 +0000713 if (!II.usesCustomDAGSchedInsertionHook()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000714 BB->insert(BB->end(), MI);
715 } else {
716 // Insert this instruction into the end of the basic block, potentially
717 // taking some custom action.
718 BB = DAG.getTargetLoweringInfo().InsertAtEndOfBasicBlock(MI, BB);
719 }
Evan Cheng26639782007-08-02 00:28:15 +0000720
721 // Additional results must be an physical register def.
722 if (HasPhysRegOuts) {
Chris Lattner0c2a4f32008-01-07 03:13:06 +0000723 for (unsigned i = II.getNumDefs(); i < NumResults; ++i) {
724 unsigned Reg = II.getImplicitDefs()[i - II.getNumDefs()];
Evan Cheng0af04f72007-08-02 05:29:38 +0000725 if (Node->hasAnyUseOfValue(i))
Evan Cheng93f143e2007-09-25 01:54:36 +0000726 EmitCopyFromReg(Node, i, InstanceNo, Reg, VRBaseMap);
Evan Cheng26639782007-08-02 00:28:15 +0000727 }
728 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000729 } else {
730 switch (Node->getOpcode()) {
731 default:
732#ifndef NDEBUG
733 Node->dump(&DAG);
734#endif
735 assert(0 && "This target-independent node should have been selected!");
736 case ISD::EntryToken: // fall thru
737 case ISD::TokenFactor:
738 case ISD::LABEL:
739 break;
740 case ISD::CopyToReg: {
741 unsigned InReg;
742 if (RegisterSDNode *R = dyn_cast<RegisterSDNode>(Node->getOperand(2)))
743 InReg = R->getReg();
744 else
745 InReg = getVR(Node->getOperand(2), VRBaseMap);
746 unsigned DestReg = cast<RegisterSDNode>(Node->getOperand(1))->getReg();
747 if (InReg != DestReg) {// Coalesced away the copy?
748 const TargetRegisterClass *TRC = 0;
749 // Get the target register class
750 if (MRegisterInfo::isVirtualRegister(InReg))
Chris Lattner1b989192007-12-31 04:13:23 +0000751 TRC = RegInfo.getRegClass(InReg);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000752 else
Evan Cheng5ec4b762007-09-26 21:36:17 +0000753 TRC =
754 MRI->getPhysicalRegisterRegClass(Node->getOperand(2).getValueType(),
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000755 InReg);
Owen Anderson8f2c8932007-12-31 06:32:00 +0000756 TII->copyRegToReg(*BB, BB->end(), DestReg, InReg, TRC, TRC);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000757 }
758 break;
759 }
760 case ISD::CopyFromReg: {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000761 unsigned SrcReg = cast<RegisterSDNode>(Node->getOperand(1))->getReg();
Evan Cheng93f143e2007-09-25 01:54:36 +0000762 EmitCopyFromReg(Node, 0, InstanceNo, SrcReg, VRBaseMap);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000763 break;
764 }
765 case ISD::INLINEASM: {
766 unsigned NumOps = Node->getNumOperands();
767 if (Node->getOperand(NumOps-1).getValueType() == MVT::Flag)
768 --NumOps; // Ignore the flag operand.
769
770 // Create the inline asm machine instruction.
771 MachineInstr *MI =
772 new MachineInstr(BB, TII->get(TargetInstrInfo::INLINEASM));
773
774 // Add the asm string as an external symbol operand.
775 const char *AsmStr =
776 cast<ExternalSymbolSDNode>(Node->getOperand(1))->getSymbol();
Chris Lattner8dfd3122007-12-30 00:51:11 +0000777 MI->addOperand(MachineOperand::CreateES(AsmStr));
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000778
779 // Add all of the operand registers to the instruction.
780 for (unsigned i = 2; i != NumOps;) {
781 unsigned Flags = cast<ConstantSDNode>(Node->getOperand(i))->getValue();
782 unsigned NumVals = Flags >> 3;
783
Chris Lattner8dfd3122007-12-30 00:51:11 +0000784 MI->addOperand(MachineOperand::CreateImm(Flags));
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000785 ++i; // Skip the ID value.
786
787 switch (Flags & 7) {
788 default: assert(0 && "Bad flags!");
789 case 1: // Use of register.
790 for (; NumVals; --NumVals, ++i) {
791 unsigned Reg = cast<RegisterSDNode>(Node->getOperand(i))->getReg();
Chris Lattner63ab1f22007-12-30 00:41:17 +0000792 MI->addOperand(MachineOperand::CreateReg(Reg, false));
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000793 }
794 break;
795 case 2: // Def of register.
796 for (; NumVals; --NumVals, ++i) {
797 unsigned Reg = cast<RegisterSDNode>(Node->getOperand(i))->getReg();
Chris Lattner63ab1f22007-12-30 00:41:17 +0000798 MI->addOperand(MachineOperand::CreateReg(Reg, true));
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000799 }
800 break;
801 case 3: { // Immediate.
Chris Lattner23544c12007-08-25 00:53:07 +0000802 for (; NumVals; --NumVals, ++i) {
803 if (ConstantSDNode *CS =
804 dyn_cast<ConstantSDNode>(Node->getOperand(i))) {
Chris Lattner63ab1f22007-12-30 00:41:17 +0000805 MI->addOperand(MachineOperand::CreateImm(CS->getValue()));
Dale Johannesencfb19e62007-11-05 21:20:28 +0000806 } else if (GlobalAddressSDNode *GA =
807 dyn_cast<GlobalAddressSDNode>(Node->getOperand(i))) {
Chris Lattner8dfd3122007-12-30 00:51:11 +0000808 MI->addOperand(MachineOperand::CreateGA(GA->getGlobal(),
809 GA->getOffset()));
Dale Johannesencfb19e62007-11-05 21:20:28 +0000810 } else {
Chris Lattner8dfd3122007-12-30 00:51:11 +0000811 BasicBlockSDNode *BB =cast<BasicBlockSDNode>(Node->getOperand(i));
812 MI->addOperand(MachineOperand::CreateMBB(BB->getBasicBlock()));
Chris Lattner23544c12007-08-25 00:53:07 +0000813 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000814 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000815 break;
816 }
817 case 4: // Addressing mode.
818 // The addressing mode has been selected, just add all of the
819 // operands to the machine instruction.
820 for (; NumVals; --NumVals, ++i)
821 AddOperand(MI, Node->getOperand(i), 0, 0, VRBaseMap);
822 break;
823 }
824 }
825 break;
826 }
827 }
828 }
829}
830
831void ScheduleDAG::EmitNoop() {
832 TII->insertNoop(*BB, BB->end());
833}
834
Evan Cheng5ec4b762007-09-26 21:36:17 +0000835void ScheduleDAG::EmitCrossRCCopy(SUnit *SU, DenseMap<SUnit*, unsigned> &VRBaseMap) {
836 for (SUnit::const_pred_iterator I = SU->Preds.begin(), E = SU->Preds.end();
837 I != E; ++I) {
838 if (I->isCtrl) continue; // ignore chain preds
839 if (!I->Dep->Node) {
840 // Copy to physical register.
841 DenseMap<SUnit*, unsigned>::iterator VRI = VRBaseMap.find(I->Dep);
842 assert(VRI != VRBaseMap.end() && "Node emitted out of order - late");
843 // Find the destination physical register.
844 unsigned Reg = 0;
845 for (SUnit::const_succ_iterator II = SU->Succs.begin(),
846 EE = SU->Succs.end(); II != EE; ++II) {
847 if (I->Reg) {
848 Reg = I->Reg;
849 break;
850 }
851 }
852 assert(I->Reg && "Unknown physical register!");
Owen Anderson8f2c8932007-12-31 06:32:00 +0000853 TII->copyRegToReg(*BB, BB->end(), Reg, VRI->second,
Evan Cheng5ec4b762007-09-26 21:36:17 +0000854 SU->CopyDstRC, SU->CopySrcRC);
855 } else {
856 // Copy from physical register.
857 assert(I->Reg && "Unknown physical register!");
Chris Lattner1b989192007-12-31 04:13:23 +0000858 unsigned VRBase = RegInfo.createVirtualRegister(SU->CopyDstRC);
Evan Cheng5ec4b762007-09-26 21:36:17 +0000859 bool isNew = VRBaseMap.insert(std::make_pair(SU, VRBase));
860 assert(isNew && "Node emitted out of order - early");
Owen Anderson8f2c8932007-12-31 06:32:00 +0000861 TII->copyRegToReg(*BB, BB->end(), VRBase, I->Reg,
Evan Cheng5ec4b762007-09-26 21:36:17 +0000862 SU->CopyDstRC, SU->CopySrcRC);
863 }
864 break;
865 }
866}
867
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000868/// EmitSchedule - Emit the machine code in scheduled order.
869void ScheduleDAG::EmitSchedule() {
870 // If this is the first basic block in the function, and if it has live ins
871 // that need to be copied into vregs, emit the copies into the top of the
872 // block before emitting the code for the block.
873 MachineFunction &MF = DAG.getMachineFunction();
Dan Gohman3f7d94b2007-10-03 19:26:29 +0000874 if (&MF.front() == BB) {
Chris Lattner1b989192007-12-31 04:13:23 +0000875 for (MachineRegisterInfo::livein_iterator LI = RegInfo.livein_begin(),
876 E = RegInfo.livein_end(); LI != E; ++LI)
Evan Chengb3d91cf2007-09-26 06:25:56 +0000877 if (LI->second) {
Chris Lattner1b989192007-12-31 04:13:23 +0000878 const TargetRegisterClass *RC = RegInfo.getRegClass(LI->second);
Owen Anderson8f2c8932007-12-31 06:32:00 +0000879 TII->copyRegToReg(*MF.begin(), MF.begin()->end(), LI->second,
Evan Chengb3d91cf2007-09-26 06:25:56 +0000880 LI->first, RC, RC);
881 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000882 }
883
884
885 // Finally, emit the code for all of the scheduled instructions.
886 DenseMap<SDOperand, unsigned> VRBaseMap;
Evan Cheng5ec4b762007-09-26 21:36:17 +0000887 DenseMap<SUnit*, unsigned> CopyVRBaseMap;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000888 for (unsigned i = 0, e = Sequence.size(); i != e; i++) {
889 if (SUnit *SU = Sequence[i]) {
Evan Cheng93f143e2007-09-25 01:54:36 +0000890 for (unsigned j = 0, ee = SU->FlaggedNodes.size(); j != ee; ++j)
891 EmitNode(SU->FlaggedNodes[j], SU->InstanceNo, VRBaseMap);
Evan Cheng5ec4b762007-09-26 21:36:17 +0000892 if (SU->Node)
893 EmitNode(SU->Node, SU->InstanceNo, VRBaseMap);
894 else
895 EmitCrossRCCopy(SU, CopyVRBaseMap);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000896 } else {
897 // Null SUnit* is a noop.
898 EmitNoop();
899 }
900 }
901}
902
903/// dump - dump the schedule.
904void ScheduleDAG::dumpSchedule() const {
905 for (unsigned i = 0, e = Sequence.size(); i != e; i++) {
906 if (SUnit *SU = Sequence[i])
907 SU->dump(&DAG);
908 else
909 cerr << "**** NOOP ****\n";
910 }
911}
912
913
914/// Run - perform scheduling.
915///
916MachineBasicBlock *ScheduleDAG::Run() {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000917 Schedule();
918 return BB;
919}
920
921/// SUnit - Scheduling unit. It's an wrapper around either a single SDNode or
922/// a group of nodes flagged together.
923void SUnit::dump(const SelectionDAG *G) const {
924 cerr << "SU(" << NodeNum << "): ";
Evan Cheng5ec4b762007-09-26 21:36:17 +0000925 if (Node)
926 Node->dump(G);
927 else
928 cerr << "CROSS RC COPY ";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000929 cerr << "\n";
930 if (FlaggedNodes.size() != 0) {
931 for (unsigned i = 0, e = FlaggedNodes.size(); i != e; i++) {
932 cerr << " ";
933 FlaggedNodes[i]->dump(G);
934 cerr << "\n";
935 }
936 }
937}
938
939void SUnit::dumpAll(const SelectionDAG *G) const {
940 dump(G);
941
942 cerr << " # preds left : " << NumPredsLeft << "\n";
943 cerr << " # succs left : " << NumSuccsLeft << "\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000944 cerr << " Latency : " << Latency << "\n";
945 cerr << " Depth : " << Depth << "\n";
946 cerr << " Height : " << Height << "\n";
947
948 if (Preds.size() != 0) {
949 cerr << " Predecessors:\n";
950 for (SUnit::const_succ_iterator I = Preds.begin(), E = Preds.end();
951 I != E; ++I) {
Evan Chenge7959472007-09-19 01:38:40 +0000952 if (I->isCtrl)
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000953 cerr << " ch #";
954 else
955 cerr << " val #";
Evan Cheng93f143e2007-09-25 01:54:36 +0000956 cerr << I->Dep << " - SU(" << I->Dep->NodeNum << ")";
957 if (I->isSpecial)
958 cerr << " *";
959 cerr << "\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000960 }
961 }
962 if (Succs.size() != 0) {
963 cerr << " Successors:\n";
964 for (SUnit::const_succ_iterator I = Succs.begin(), E = Succs.end();
965 I != E; ++I) {
Evan Chenge7959472007-09-19 01:38:40 +0000966 if (I->isCtrl)
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000967 cerr << " ch #";
968 else
969 cerr << " val #";
Evan Cheng93f143e2007-09-25 01:54:36 +0000970 cerr << I->Dep << " - SU(" << I->Dep->NodeNum << ")";
971 if (I->isSpecial)
972 cerr << " *";
973 cerr << "\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000974 }
975 }
976 cerr << "\n";
977}