blob: 5f09a4f6b145da0ccc850bc0ffdfb0b199a76275 [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();
Dan Gohman1e57df32008-02-10 18:45:23 +000035 TRI = TM.getRegisterInfo();
Chris Lattner1b989192007-12-31 04:13:23 +000036 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,
Dan Gohman1e57df32008-02-10 18:45:23 +000043 const TargetRegisterInfo *TRI,
Evan Cheng93f143e2007-09-25 01:54:36 +000044 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();
Dan Gohman1e57df32008-02-10 18:45:23 +000050 if (TargetRegisterInfo::isVirtualRegister(Reg))
Evan Cheng93f143e2007-09-25 01:54:36 +000051 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 =
Dan Gohman1e57df32008-02-10 18:45:23 +000060 TRI->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.
Dan Gohman1e57df32008-02-10 18:45:23 +0000188 CheckForPhysRegDependency(OpN, N, i, TRI, TII, PhysReg, Cost);
Evan Cheng93f143e2007-09-25 01:54:36 +0000189 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
Dan Gohman0256f1e2008-02-11 19:00:03 +0000270/// not go into the resulting MachineInstr).
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000271unsigned 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 Gohman12a9c082008-02-06 22:27:42 +0000280/// CountOperands - The inputs to target nodes have any actual inputs first,
281/// followed by optional memory operands chain operand, then flag operands.
Dan Gohman0256f1e2008-02-11 19:00:03 +0000282/// Compute the number of actual operands that will go into the resulting
283/// MachineInstr.
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000284unsigned ScheduleDAG::CountOperands(SDNode *Node) {
285 unsigned N = Node->getNumOperands();
286 while (N && Node->getOperand(N - 1).getValueType() == MVT::Flag)
287 --N;
288 if (N && Node->getOperand(N - 1).getValueType() == MVT::Other)
289 --N; // Ignore chain if it exists.
Dan Gohman12a9c082008-02-06 22:27:42 +0000290 while (N && MemOperandSDNode::classof(Node->getOperand(N - 1).Val))
291 --N; // Ignore MemOperand nodes
292 return N;
293}
294
295/// CountMemOperands - Find the index of the last MemOperandSDNode operand
296unsigned ScheduleDAG::CountMemOperands(SDNode *Node) {
297 unsigned N = Node->getNumOperands();
298 while (N && Node->getOperand(N - 1).getValueType() == MVT::Flag)
299 --N;
300 if (N && Node->getOperand(N - 1).getValueType() == MVT::Other)
301 --N; // Ignore chain if it exists.
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000302 return N;
303}
304
305static const TargetRegisterClass *getInstrOperandRegClass(
Dan Gohman1e57df32008-02-10 18:45:23 +0000306 const TargetRegisterInfo *TRI,
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000307 const TargetInstrInfo *TII,
Chris Lattner5b930372008-01-07 07:27:27 +0000308 const TargetInstrDesc &II,
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000309 unsigned Op) {
Chris Lattner5b930372008-01-07 07:27:27 +0000310 if (Op >= II.getNumOperands()) {
311 assert(II.isVariadic() && "Invalid operand # of instruction");
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000312 return NULL;
313 }
Chris Lattner5b930372008-01-07 07:27:27 +0000314 if (II.OpInfo[Op].isLookupPtrRegClass())
Chris Lattnereeedb482008-01-07 02:39:19 +0000315 return TII->getPointerRegClass();
Dan Gohman1e57df32008-02-10 18:45:23 +0000316 return TRI->getRegClass(II.OpInfo[Op].RegClass);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000317}
318
Evan Cheng93f143e2007-09-25 01:54:36 +0000319void ScheduleDAG::EmitCopyFromReg(SDNode *Node, unsigned ResNo,
320 unsigned InstanceNo, unsigned SrcReg,
Evan Cheng26639782007-08-02 00:28:15 +0000321 DenseMap<SDOperand, unsigned> &VRBaseMap) {
322 unsigned VRBase = 0;
Dan Gohman1e57df32008-02-10 18:45:23 +0000323 if (TargetRegisterInfo::isVirtualRegister(SrcReg)) {
Evan Cheng26639782007-08-02 00:28:15 +0000324 // Just use the input register directly!
Evan Cheng93f143e2007-09-25 01:54:36 +0000325 if (InstanceNo > 0)
326 VRBaseMap.erase(SDOperand(Node, ResNo));
Evan Cheng26639782007-08-02 00:28:15 +0000327 bool isNew = VRBaseMap.insert(std::make_pair(SDOperand(Node,ResNo),SrcReg));
328 assert(isNew && "Node emitted out of order - early");
329 return;
330 }
331
332 // If the node is only used by a CopyToReg and the dest reg is a vreg, use
333 // the CopyToReg'd destination register instead of creating a new vreg.
Evan Cheng93f143e2007-09-25 01:54:36 +0000334 bool MatchReg = true;
Evan Cheng26639782007-08-02 00:28:15 +0000335 for (SDNode::use_iterator UI = Node->use_begin(), E = Node->use_end();
336 UI != E; ++UI) {
337 SDNode *Use = *UI;
Evan Cheng93f143e2007-09-25 01:54:36 +0000338 bool Match = true;
Evan Cheng26639782007-08-02 00:28:15 +0000339 if (Use->getOpcode() == ISD::CopyToReg &&
340 Use->getOperand(2).Val == Node &&
341 Use->getOperand(2).ResNo == ResNo) {
342 unsigned DestReg = cast<RegisterSDNode>(Use->getOperand(1))->getReg();
Dan Gohman1e57df32008-02-10 18:45:23 +0000343 if (TargetRegisterInfo::isVirtualRegister(DestReg)) {
Evan Cheng26639782007-08-02 00:28:15 +0000344 VRBase = DestReg;
Evan Cheng93f143e2007-09-25 01:54:36 +0000345 Match = false;
346 } else if (DestReg != SrcReg)
347 Match = false;
348 } else {
349 for (unsigned i = 0, e = Use->getNumOperands(); i != e; ++i) {
350 SDOperand Op = Use->getOperand(i);
Evan Cheng4f0345c2007-12-14 08:25:15 +0000351 if (Op.Val != Node || Op.ResNo != ResNo)
Evan Cheng93f143e2007-09-25 01:54:36 +0000352 continue;
353 MVT::ValueType VT = Node->getValueType(Op.ResNo);
354 if (VT != MVT::Other && VT != MVT::Flag)
355 Match = false;
Evan Cheng26639782007-08-02 00:28:15 +0000356 }
357 }
Evan Cheng93f143e2007-09-25 01:54:36 +0000358 MatchReg &= Match;
359 if (VRBase)
360 break;
Evan Cheng26639782007-08-02 00:28:15 +0000361 }
362
Evan Cheng26639782007-08-02 00:28:15 +0000363 const TargetRegisterClass *TRC = 0;
Evan Cheng93f143e2007-09-25 01:54:36 +0000364 // Figure out the register class to create for the destreg.
365 if (VRBase)
Chris Lattner1b989192007-12-31 04:13:23 +0000366 TRC = RegInfo.getRegClass(VRBase);
Evan Cheng93f143e2007-09-25 01:54:36 +0000367 else
Dan Gohman1e57df32008-02-10 18:45:23 +0000368 TRC = TRI->getPhysicalRegisterRegClass(Node->getValueType(ResNo), SrcReg);
Evan Cheng93f143e2007-09-25 01:54:36 +0000369
370 // If all uses are reading from the src physical register and copying the
371 // register is either impossible or very expensive, then don't create a copy.
372 if (MatchReg && TRC->getCopyCost() < 0) {
373 VRBase = SrcReg;
374 } else {
Evan Cheng26639782007-08-02 00:28:15 +0000375 // Create the reg, emit the copy.
Chris Lattner1b989192007-12-31 04:13:23 +0000376 VRBase = RegInfo.createVirtualRegister(TRC);
Owen Anderson8f2c8932007-12-31 06:32:00 +0000377 TII->copyRegToReg(*BB, BB->end(), VRBase, SrcReg, TRC, TRC);
Evan Cheng26639782007-08-02 00:28:15 +0000378 }
Evan Cheng26639782007-08-02 00:28:15 +0000379
Evan Cheng93f143e2007-09-25 01:54:36 +0000380 if (InstanceNo > 0)
381 VRBaseMap.erase(SDOperand(Node, ResNo));
Evan Cheng26639782007-08-02 00:28:15 +0000382 bool isNew = VRBaseMap.insert(std::make_pair(SDOperand(Node,ResNo), VRBase));
383 assert(isNew && "Node emitted out of order - early");
384}
385
386void ScheduleDAG::CreateVirtualRegisters(SDNode *Node,
387 MachineInstr *MI,
Chris Lattner5b930372008-01-07 07:27:27 +0000388 const TargetInstrDesc &II,
Evan Cheng26639782007-08-02 00:28:15 +0000389 DenseMap<SDOperand, unsigned> &VRBaseMap) {
Chris Lattner0c2a4f32008-01-07 03:13:06 +0000390 for (unsigned i = 0; i < II.getNumDefs(); ++i) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000391 // If the specific node value is only used by a CopyToReg and the dest reg
392 // is a vreg, use the CopyToReg'd destination register instead of creating
393 // a new vreg.
394 unsigned VRBase = 0;
395 for (SDNode::use_iterator UI = Node->use_begin(), E = Node->use_end();
396 UI != E; ++UI) {
397 SDNode *Use = *UI;
398 if (Use->getOpcode() == ISD::CopyToReg &&
399 Use->getOperand(2).Val == Node &&
400 Use->getOperand(2).ResNo == i) {
401 unsigned Reg = cast<RegisterSDNode>(Use->getOperand(1))->getReg();
Dan Gohman1e57df32008-02-10 18:45:23 +0000402 if (TargetRegisterInfo::isVirtualRegister(Reg)) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000403 VRBase = Reg;
Chris Lattner63ab1f22007-12-30 00:41:17 +0000404 MI->addOperand(MachineOperand::CreateReg(Reg, true));
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000405 break;
406 }
407 }
408 }
409
Evan Cheng26639782007-08-02 00:28:15 +0000410 // Create the result registers for this node and add the result regs to
411 // the machine instruction.
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000412 if (VRBase == 0) {
Dan Gohman1e57df32008-02-10 18:45:23 +0000413 const TargetRegisterClass *RC = getInstrOperandRegClass(TRI, TII, II, i);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000414 assert(RC && "Isn't a register operand!");
Chris Lattner1b989192007-12-31 04:13:23 +0000415 VRBase = RegInfo.createVirtualRegister(RC);
Chris Lattner63ab1f22007-12-30 00:41:17 +0000416 MI->addOperand(MachineOperand::CreateReg(VRBase, true));
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000417 }
418
419 bool isNew = VRBaseMap.insert(std::make_pair(SDOperand(Node,i), VRBase));
420 assert(isNew && "Node emitted out of order - early");
421 }
422}
423
424/// getVR - Return the virtual register corresponding to the specified result
425/// of the specified node.
426static unsigned getVR(SDOperand Op, DenseMap<SDOperand, unsigned> &VRBaseMap) {
427 DenseMap<SDOperand, unsigned>::iterator I = VRBaseMap.find(Op);
428 assert(I != VRBaseMap.end() && "Node emitted out of order - late");
429 return I->second;
430}
431
432
433/// AddOperand - Add the specified operand to the specified machine instr. II
434/// specifies the instruction information for the node, and IIOpNum is the
435/// operand number (in the II) that we are adding. IIOpNum and II are used for
436/// assertions only.
437void ScheduleDAG::AddOperand(MachineInstr *MI, SDOperand Op,
438 unsigned IIOpNum,
Chris Lattner5b930372008-01-07 07:27:27 +0000439 const TargetInstrDesc *II,
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000440 DenseMap<SDOperand, unsigned> &VRBaseMap) {
441 if (Op.isTargetOpcode()) {
442 // Note that this case is redundant with the final else block, but we
443 // include it because it is the most common and it makes the logic
444 // simpler here.
445 assert(Op.getValueType() != MVT::Other &&
446 Op.getValueType() != MVT::Flag &&
447 "Chain and flag operands should occur at end of operand list!");
448
449 // Get/emit the operand.
450 unsigned VReg = getVR(Op, VRBaseMap);
Chris Lattner5b930372008-01-07 07:27:27 +0000451 const TargetInstrDesc &TID = MI->getDesc();
452 bool isOptDef = (IIOpNum < TID.getNumOperands())
453 ? (TID.OpInfo[IIOpNum].isOptionalDef()) : false;
Chris Lattner63ab1f22007-12-30 00:41:17 +0000454 MI->addOperand(MachineOperand::CreateReg(VReg, isOptDef));
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000455
456 // Verify that it is right.
Dan Gohman1e57df32008-02-10 18:45:23 +0000457 assert(TargetRegisterInfo::isVirtualRegister(VReg) && "Not a vreg?");
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000458 if (II) {
459 const TargetRegisterClass *RC =
Dan Gohman1e57df32008-02-10 18:45:23 +0000460 getInstrOperandRegClass(TRI, TII, *II, IIOpNum);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000461 assert(RC && "Don't have operand info for this instruction!");
Chris Lattner1b989192007-12-31 04:13:23 +0000462 const TargetRegisterClass *VRC = RegInfo.getRegClass(VReg);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000463 if (VRC != RC) {
464 cerr << "Register class of operand and regclass of use don't agree!\n";
465#ifndef NDEBUG
466 cerr << "Operand = " << IIOpNum << "\n";
467 cerr << "Op->Val = "; Op.Val->dump(&DAG); cerr << "\n";
468 cerr << "MI = "; MI->print(cerr);
469 cerr << "VReg = " << VReg << "\n";
470 cerr << "VReg RegClass size = " << VRC->getSize()
471 << ", align = " << VRC->getAlignment() << "\n";
472 cerr << "Expected RegClass size = " << RC->getSize()
473 << ", align = " << RC->getAlignment() << "\n";
474#endif
475 cerr << "Fatal error, aborting.\n";
476 abort();
477 }
478 }
Chris Lattner8dfd3122007-12-30 00:51:11 +0000479 } else if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Chris Lattner63ab1f22007-12-30 00:41:17 +0000480 MI->addOperand(MachineOperand::CreateImm(C->getValue()));
Chris Lattner8dfd3122007-12-30 00:51:11 +0000481 } else if (RegisterSDNode *R = dyn_cast<RegisterSDNode>(Op)) {
Chris Lattner63ab1f22007-12-30 00:41:17 +0000482 MI->addOperand(MachineOperand::CreateReg(R->getReg(), false));
Chris Lattner8dfd3122007-12-30 00:51:11 +0000483 } else if (GlobalAddressSDNode *TGA = dyn_cast<GlobalAddressSDNode>(Op)) {
484 MI->addOperand(MachineOperand::CreateGA(TGA->getGlobal(),TGA->getOffset()));
485 } else if (BasicBlockSDNode *BB = dyn_cast<BasicBlockSDNode>(Op)) {
486 MI->addOperand(MachineOperand::CreateMBB(BB->getBasicBlock()));
487 } else if (FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(Op)) {
488 MI->addOperand(MachineOperand::CreateFI(FI->getIndex()));
489 } else if (JumpTableSDNode *JT = dyn_cast<JumpTableSDNode>(Op)) {
490 MI->addOperand(MachineOperand::CreateJTI(JT->getIndex()));
491 } else if (ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(Op)) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000492 int Offset = CP->getOffset();
493 unsigned Align = CP->getAlignment();
494 const Type *Type = CP->getType();
495 // MachineConstantPool wants an explicit alignment.
496 if (Align == 0) {
497 Align = TM.getTargetData()->getPreferredTypeAlignmentShift(Type);
498 if (Align == 0) {
499 // Alignment of vector types. FIXME!
Duncan Sandsf99fdc62007-11-01 20:53:16 +0000500 Align = TM.getTargetData()->getABITypeSize(Type);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000501 Align = Log2_64(Align);
502 }
503 }
504
505 unsigned Idx;
506 if (CP->isMachineConstantPoolEntry())
507 Idx = ConstPool->getConstantPoolIndex(CP->getMachineCPVal(), Align);
508 else
509 Idx = ConstPool->getConstantPoolIndex(CP->getConstVal(), Align);
Chris Lattner8dfd3122007-12-30 00:51:11 +0000510 MI->addOperand(MachineOperand::CreateCPI(Idx, Offset));
511 } else if (ExternalSymbolSDNode *ES = dyn_cast<ExternalSymbolSDNode>(Op)) {
512 MI->addOperand(MachineOperand::CreateES(ES->getSymbol()));
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000513 } else {
514 assert(Op.getValueType() != MVT::Other &&
515 Op.getValueType() != MVT::Flag &&
516 "Chain and flag operands should occur at end of operand list!");
517 unsigned VReg = getVR(Op, VRBaseMap);
Chris Lattner63ab1f22007-12-30 00:41:17 +0000518 MI->addOperand(MachineOperand::CreateReg(VReg, false));
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000519
520 // Verify that it is right.
Dan Gohman1e57df32008-02-10 18:45:23 +0000521 assert(TargetRegisterInfo::isVirtualRegister(VReg) && "Not a vreg?");
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000522 if (II) {
523 const TargetRegisterClass *RC =
Dan Gohman1e57df32008-02-10 18:45:23 +0000524 getInstrOperandRegClass(TRI, TII, *II, IIOpNum);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000525 assert(RC && "Don't have operand info for this instruction!");
Chris Lattner1b989192007-12-31 04:13:23 +0000526 assert(RegInfo.getRegClass(VReg) == RC &&
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000527 "Register class of operand and regclass of use don't agree!");
528 }
529 }
530
531}
532
Dan Gohman12a9c082008-02-06 22:27:42 +0000533void ScheduleDAG::AddMemOperand(MachineInstr *MI, const MemOperand &MO) {
534 MI->addMemOperand(MO);
535}
536
Christopher Lambe95328d2007-07-26 08:12:07 +0000537// Returns the Register Class of a subregister
538static const TargetRegisterClass *getSubRegisterRegClass(
539 const TargetRegisterClass *TRC,
540 unsigned SubIdx) {
541 // Pick the register class of the subregister
Dan Gohman1e57df32008-02-10 18:45:23 +0000542 TargetRegisterInfo::regclass_iterator I =
543 TRC->subregclasses_begin() + SubIdx-1;
Christopher Lambe95328d2007-07-26 08:12:07 +0000544 assert(I < TRC->subregclasses_end() &&
545 "Invalid subregister index for register class");
546 return *I;
547}
548
549static const TargetRegisterClass *getSuperregRegisterClass(
550 const TargetRegisterClass *TRC,
551 unsigned SubIdx,
552 MVT::ValueType VT) {
553 // Pick the register class of the superegister for this type
Dan Gohman1e57df32008-02-10 18:45:23 +0000554 for (TargetRegisterInfo::regclass_iterator I = TRC->superregclasses_begin(),
Christopher Lambe95328d2007-07-26 08:12:07 +0000555 E = TRC->superregclasses_end(); I != E; ++I)
556 if ((*I)->hasType(VT) && getSubRegisterRegClass(*I, SubIdx) == TRC)
557 return *I;
558 assert(false && "Couldn't find the register class");
559 return 0;
560}
561
562/// EmitSubregNode - Generate machine code for subreg nodes.
563///
564void ScheduleDAG::EmitSubregNode(SDNode *Node,
565 DenseMap<SDOperand, unsigned> &VRBaseMap) {
566 unsigned VRBase = 0;
567 unsigned Opc = Node->getTargetOpcode();
568 if (Opc == TargetInstrInfo::EXTRACT_SUBREG) {
569 // If the node is only used by a CopyToReg and the dest reg is a vreg, use
570 // the CopyToReg'd destination register instead of creating a new vreg.
571 for (SDNode::use_iterator UI = Node->use_begin(), E = Node->use_end();
572 UI != E; ++UI) {
573 SDNode *Use = *UI;
574 if (Use->getOpcode() == ISD::CopyToReg &&
575 Use->getOperand(2).Val == Node) {
576 unsigned DestReg = cast<RegisterSDNode>(Use->getOperand(1))->getReg();
Dan Gohman1e57df32008-02-10 18:45:23 +0000577 if (TargetRegisterInfo::isVirtualRegister(DestReg)) {
Christopher Lambe95328d2007-07-26 08:12:07 +0000578 VRBase = DestReg;
579 break;
580 }
581 }
582 }
583
584 unsigned SubIdx = cast<ConstantSDNode>(Node->getOperand(1))->getValue();
585
586 // TODO: If the node is a use of a CopyFromReg from a physical register
587 // fold the extract into the copy now
588
Christopher Lambe95328d2007-07-26 08:12:07 +0000589 // Create the extract_subreg machine instruction.
590 MachineInstr *MI =
591 new MachineInstr(BB, TII->get(TargetInstrInfo::EXTRACT_SUBREG));
592
593 // Figure out the register class to create for the destreg.
594 unsigned VReg = getVR(Node->getOperand(0), VRBaseMap);
Chris Lattner1b989192007-12-31 04:13:23 +0000595 const TargetRegisterClass *TRC = RegInfo.getRegClass(VReg);
Christopher Lambe95328d2007-07-26 08:12:07 +0000596 const TargetRegisterClass *SRC = getSubRegisterRegClass(TRC, SubIdx);
597
598 if (VRBase) {
599 // Grab the destination register
600 const TargetRegisterClass *DRC = 0;
Chris Lattner1b989192007-12-31 04:13:23 +0000601 DRC = RegInfo.getRegClass(VRBase);
Christopher Lambe08d9ec2008-01-31 07:09:08 +0000602 assert(SRC && DRC && SRC == DRC &&
Christopher Lambe95328d2007-07-26 08:12:07 +0000603 "Source subregister and destination must have the same class");
604 } else {
605 // Create the reg
Christopher Lambe08d9ec2008-01-31 07:09:08 +0000606 assert(SRC && "Couldn't find source register class");
Chris Lattner1b989192007-12-31 04:13:23 +0000607 VRBase = RegInfo.createVirtualRegister(SRC);
Christopher Lambe95328d2007-07-26 08:12:07 +0000608 }
609
610 // Add def, source, and subreg index
Chris Lattner63ab1f22007-12-30 00:41:17 +0000611 MI->addOperand(MachineOperand::CreateReg(VRBase, true));
Christopher Lambe95328d2007-07-26 08:12:07 +0000612 AddOperand(MI, Node->getOperand(0), 0, 0, VRBaseMap);
Chris Lattner8dfd3122007-12-30 00:51:11 +0000613 MI->addOperand(MachineOperand::CreateImm(SubIdx));
Christopher Lambe95328d2007-07-26 08:12:07 +0000614
615 } else if (Opc == TargetInstrInfo::INSERT_SUBREG) {
616 assert((Node->getNumOperands() == 2 || Node->getNumOperands() == 3) &&
617 "Malformed insert_subreg node");
618 bool isUndefInput = (Node->getNumOperands() == 2);
619 unsigned SubReg = 0;
620 unsigned SubIdx = 0;
621
622 if (isUndefInput) {
623 SubReg = getVR(Node->getOperand(0), VRBaseMap);
624 SubIdx = cast<ConstantSDNode>(Node->getOperand(1))->getValue();
625 } else {
626 SubReg = getVR(Node->getOperand(1), VRBaseMap);
627 SubIdx = cast<ConstantSDNode>(Node->getOperand(2))->getValue();
628 }
629
Chris Lattnerb70e1512007-12-31 04:16:08 +0000630 // TODO: Add tracking info to MachineRegisterInfo of which vregs are subregs
Christopher Lambe95328d2007-07-26 08:12:07 +0000631 // to allow coalescing in the allocator
632
633 // If the node is only used by a CopyToReg and the dest reg is a vreg, use
634 // the CopyToReg'd destination register instead of creating a new vreg.
635 // If the CopyToReg'd destination register is physical, then fold the
636 // insert into the copy
637 for (SDNode::use_iterator UI = Node->use_begin(), E = Node->use_end();
638 UI != E; ++UI) {
639 SDNode *Use = *UI;
640 if (Use->getOpcode() == ISD::CopyToReg &&
641 Use->getOperand(2).Val == Node) {
642 unsigned DestReg = cast<RegisterSDNode>(Use->getOperand(1))->getReg();
Dan Gohman1e57df32008-02-10 18:45:23 +0000643 if (TargetRegisterInfo::isVirtualRegister(DestReg)) {
Christopher Lambe95328d2007-07-26 08:12:07 +0000644 VRBase = DestReg;
645 break;
646 }
647 }
648 }
649
650 // Create the insert_subreg machine instruction.
651 MachineInstr *MI =
652 new MachineInstr(BB, TII->get(TargetInstrInfo::INSERT_SUBREG));
653
654 // Figure out the register class to create for the destreg.
655 const TargetRegisterClass *TRC = 0;
656 if (VRBase) {
Chris Lattner1b989192007-12-31 04:13:23 +0000657 TRC = RegInfo.getRegClass(VRBase);
Christopher Lambe95328d2007-07-26 08:12:07 +0000658 } else {
Chris Lattner1b989192007-12-31 04:13:23 +0000659 TRC = getSuperregRegisterClass(RegInfo.getRegClass(SubReg), SubIdx,
Christopher Lambe95328d2007-07-26 08:12:07 +0000660 Node->getValueType(0));
661 assert(TRC && "Couldn't determine register class for insert_subreg");
Chris Lattner1b989192007-12-31 04:13:23 +0000662 VRBase = RegInfo.createVirtualRegister(TRC); // Create the reg
Christopher Lambe95328d2007-07-26 08:12:07 +0000663 }
664
Chris Lattner63ab1f22007-12-30 00:41:17 +0000665 MI->addOperand(MachineOperand::CreateReg(VRBase, true));
Christopher Lambe95328d2007-07-26 08:12:07 +0000666 AddOperand(MI, Node->getOperand(0), 0, 0, VRBaseMap);
667 if (!isUndefInput)
668 AddOperand(MI, Node->getOperand(1), 0, 0, VRBaseMap);
Chris Lattner8dfd3122007-12-30 00:51:11 +0000669 MI->addOperand(MachineOperand::CreateImm(SubIdx));
Christopher Lambe95328d2007-07-26 08:12:07 +0000670 } else
671 assert(0 && "Node is not a subreg insert or extract");
672
673 bool isNew = VRBaseMap.insert(std::make_pair(SDOperand(Node,0), VRBase));
674 assert(isNew && "Node emitted out of order - early");
675}
676
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000677/// EmitNode - Generate machine code for an node and needed dependencies.
678///
Evan Cheng93f143e2007-09-25 01:54:36 +0000679void ScheduleDAG::EmitNode(SDNode *Node, unsigned InstanceNo,
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000680 DenseMap<SDOperand, unsigned> &VRBaseMap) {
681 // If machine instruction
682 if (Node->isTargetOpcode()) {
683 unsigned Opc = Node->getTargetOpcode();
Christopher Lambe95328d2007-07-26 08:12:07 +0000684
685 // Handle subreg insert/extract specially
686 if (Opc == TargetInstrInfo::EXTRACT_SUBREG ||
687 Opc == TargetInstrInfo::INSERT_SUBREG) {
688 EmitSubregNode(Node, VRBaseMap);
689 return;
690 }
691
Chris Lattner5b930372008-01-07 07:27:27 +0000692 const TargetInstrDesc &II = TII->get(Opc);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000693
694 unsigned NumResults = CountResults(Node);
695 unsigned NodeOperands = CountOperands(Node);
Dan Gohman12a9c082008-02-06 22:27:42 +0000696 unsigned NodeMemOperands = CountMemOperands(Node);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000697 unsigned NumMIOperands = NodeOperands + NumResults;
Chris Lattner0c2a4f32008-01-07 03:13:06 +0000698 bool HasPhysRegOuts = (NumResults > II.getNumDefs()) &&
699 II.getImplicitDefs() != 0;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000700#ifndef NDEBUG
Chris Lattner0c2a4f32008-01-07 03:13:06 +0000701 assert((II.getNumOperands() == NumMIOperands ||
Chris Lattner2fb37c02008-01-07 05:19:29 +0000702 HasPhysRegOuts || II.isVariadic()) &&
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000703 "#operands for dag node doesn't match .td file!");
704#endif
705
706 // Create the new machine instruction.
707 MachineInstr *MI = new MachineInstr(II);
708
709 // Add result register values for things that are defined by this
710 // instruction.
711 if (NumResults)
Evan Cheng26639782007-08-02 00:28:15 +0000712 CreateVirtualRegisters(Node, MI, II, VRBaseMap);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000713
714 // Emit all of the actual operands of this instruction, adding them to the
715 // instruction as appropriate.
716 for (unsigned i = 0; i != NodeOperands; ++i)
Chris Lattner0c2a4f32008-01-07 03:13:06 +0000717 AddOperand(MI, Node->getOperand(i), i+II.getNumDefs(), &II, VRBaseMap);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000718
Dan Gohman12a9c082008-02-06 22:27:42 +0000719 // Emit all of the memory operands of this instruction
720 for (unsigned i = NodeOperands; i != NodeMemOperands; ++i)
721 AddMemOperand(MI, cast<MemOperandSDNode>(Node->getOperand(i))->MO);
722
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000723 // Commute node if it has been determined to be profitable.
724 if (CommuteSet.count(Node)) {
725 MachineInstr *NewMI = TII->commuteInstruction(MI);
726 if (NewMI == 0)
727 DOUT << "Sched: COMMUTING FAILED!\n";
728 else {
729 DOUT << "Sched: COMMUTED TO: " << *NewMI;
730 if (MI != NewMI) {
731 delete MI;
732 MI = NewMI;
733 }
734 }
735 }
736
Evan Chenga53c40a2008-02-01 09:10:45 +0000737 if (II.usesCustomDAGSchedInsertionHook())
Evan Cheng2d373922008-01-30 19:35:32 +0000738 // Insert this instruction into the basic block using a target
739 // specific inserter which may returns a new basic block.
Evan Chenge637db12008-01-30 18:18:23 +0000740 BB = DAG.getTargetLoweringInfo().EmitInstrWithCustomInserter(MI, BB);
Evan Cheng2d373922008-01-30 19:35:32 +0000741 else
742 BB->push_back(MI);
Evan Cheng26639782007-08-02 00:28:15 +0000743
744 // Additional results must be an physical register def.
745 if (HasPhysRegOuts) {
Chris Lattner0c2a4f32008-01-07 03:13:06 +0000746 for (unsigned i = II.getNumDefs(); i < NumResults; ++i) {
747 unsigned Reg = II.getImplicitDefs()[i - II.getNumDefs()];
Evan Cheng0af04f72007-08-02 05:29:38 +0000748 if (Node->hasAnyUseOfValue(i))
Evan Cheng93f143e2007-09-25 01:54:36 +0000749 EmitCopyFromReg(Node, i, InstanceNo, Reg, VRBaseMap);
Evan Cheng26639782007-08-02 00:28:15 +0000750 }
751 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000752 } else {
753 switch (Node->getOpcode()) {
754 default:
755#ifndef NDEBUG
756 Node->dump(&DAG);
757#endif
758 assert(0 && "This target-independent node should have been selected!");
759 case ISD::EntryToken: // fall thru
760 case ISD::TokenFactor:
761 case ISD::LABEL:
Evan Cheng2e28d622008-02-02 04:07:54 +0000762 case ISD::DECLARE:
Dan Gohman12a9c082008-02-06 22:27:42 +0000763 case ISD::SRCVALUE:
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000764 break;
765 case ISD::CopyToReg: {
766 unsigned InReg;
767 if (RegisterSDNode *R = dyn_cast<RegisterSDNode>(Node->getOperand(2)))
768 InReg = R->getReg();
769 else
770 InReg = getVR(Node->getOperand(2), VRBaseMap);
771 unsigned DestReg = cast<RegisterSDNode>(Node->getOperand(1))->getReg();
772 if (InReg != DestReg) {// Coalesced away the copy?
773 const TargetRegisterClass *TRC = 0;
774 // Get the target register class
Dan Gohman1e57df32008-02-10 18:45:23 +0000775 if (TargetRegisterInfo::isVirtualRegister(InReg))
Chris Lattner1b989192007-12-31 04:13:23 +0000776 TRC = RegInfo.getRegClass(InReg);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000777 else
Evan Cheng5ec4b762007-09-26 21:36:17 +0000778 TRC =
Dan Gohman1e57df32008-02-10 18:45:23 +0000779 TRI->getPhysicalRegisterRegClass(Node->getOperand(2).getValueType(),
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000780 InReg);
Owen Anderson8f2c8932007-12-31 06:32:00 +0000781 TII->copyRegToReg(*BB, BB->end(), DestReg, InReg, TRC, TRC);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000782 }
783 break;
784 }
785 case ISD::CopyFromReg: {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000786 unsigned SrcReg = cast<RegisterSDNode>(Node->getOperand(1))->getReg();
Evan Cheng93f143e2007-09-25 01:54:36 +0000787 EmitCopyFromReg(Node, 0, InstanceNo, SrcReg, VRBaseMap);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000788 break;
789 }
790 case ISD::INLINEASM: {
791 unsigned NumOps = Node->getNumOperands();
792 if (Node->getOperand(NumOps-1).getValueType() == MVT::Flag)
793 --NumOps; // Ignore the flag operand.
794
795 // Create the inline asm machine instruction.
796 MachineInstr *MI =
797 new MachineInstr(BB, TII->get(TargetInstrInfo::INLINEASM));
798
799 // Add the asm string as an external symbol operand.
800 const char *AsmStr =
801 cast<ExternalSymbolSDNode>(Node->getOperand(1))->getSymbol();
Chris Lattner8dfd3122007-12-30 00:51:11 +0000802 MI->addOperand(MachineOperand::CreateES(AsmStr));
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000803
804 // Add all of the operand registers to the instruction.
805 for (unsigned i = 2; i != NumOps;) {
806 unsigned Flags = cast<ConstantSDNode>(Node->getOperand(i))->getValue();
807 unsigned NumVals = Flags >> 3;
808
Chris Lattner8dfd3122007-12-30 00:51:11 +0000809 MI->addOperand(MachineOperand::CreateImm(Flags));
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000810 ++i; // Skip the ID value.
811
812 switch (Flags & 7) {
813 default: assert(0 && "Bad flags!");
814 case 1: // Use of register.
815 for (; NumVals; --NumVals, ++i) {
816 unsigned Reg = cast<RegisterSDNode>(Node->getOperand(i))->getReg();
Chris Lattner63ab1f22007-12-30 00:41:17 +0000817 MI->addOperand(MachineOperand::CreateReg(Reg, false));
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000818 }
819 break;
820 case 2: // Def of register.
821 for (; NumVals; --NumVals, ++i) {
822 unsigned Reg = cast<RegisterSDNode>(Node->getOperand(i))->getReg();
Chris Lattner63ab1f22007-12-30 00:41:17 +0000823 MI->addOperand(MachineOperand::CreateReg(Reg, true));
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000824 }
825 break;
826 case 3: { // Immediate.
Chris Lattner23544c12007-08-25 00:53:07 +0000827 for (; NumVals; --NumVals, ++i) {
828 if (ConstantSDNode *CS =
829 dyn_cast<ConstantSDNode>(Node->getOperand(i))) {
Chris Lattner63ab1f22007-12-30 00:41:17 +0000830 MI->addOperand(MachineOperand::CreateImm(CS->getValue()));
Dale Johannesencfb19e62007-11-05 21:20:28 +0000831 } else if (GlobalAddressSDNode *GA =
832 dyn_cast<GlobalAddressSDNode>(Node->getOperand(i))) {
Chris Lattner8dfd3122007-12-30 00:51:11 +0000833 MI->addOperand(MachineOperand::CreateGA(GA->getGlobal(),
834 GA->getOffset()));
Dale Johannesencfb19e62007-11-05 21:20:28 +0000835 } else {
Chris Lattner8dfd3122007-12-30 00:51:11 +0000836 BasicBlockSDNode *BB =cast<BasicBlockSDNode>(Node->getOperand(i));
837 MI->addOperand(MachineOperand::CreateMBB(BB->getBasicBlock()));
Chris Lattner23544c12007-08-25 00:53:07 +0000838 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000839 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000840 break;
841 }
842 case 4: // Addressing mode.
843 // The addressing mode has been selected, just add all of the
844 // operands to the machine instruction.
845 for (; NumVals; --NumVals, ++i)
846 AddOperand(MI, Node->getOperand(i), 0, 0, VRBaseMap);
847 break;
848 }
849 }
850 break;
851 }
852 }
853 }
854}
855
856void ScheduleDAG::EmitNoop() {
857 TII->insertNoop(*BB, BB->end());
858}
859
Evan Cheng5ec4b762007-09-26 21:36:17 +0000860void ScheduleDAG::EmitCrossRCCopy(SUnit *SU, DenseMap<SUnit*, unsigned> &VRBaseMap) {
861 for (SUnit::const_pred_iterator I = SU->Preds.begin(), E = SU->Preds.end();
862 I != E; ++I) {
863 if (I->isCtrl) continue; // ignore chain preds
864 if (!I->Dep->Node) {
865 // Copy to physical register.
866 DenseMap<SUnit*, unsigned>::iterator VRI = VRBaseMap.find(I->Dep);
867 assert(VRI != VRBaseMap.end() && "Node emitted out of order - late");
868 // Find the destination physical register.
869 unsigned Reg = 0;
870 for (SUnit::const_succ_iterator II = SU->Succs.begin(),
871 EE = SU->Succs.end(); II != EE; ++II) {
872 if (I->Reg) {
873 Reg = I->Reg;
874 break;
875 }
876 }
877 assert(I->Reg && "Unknown physical register!");
Owen Anderson8f2c8932007-12-31 06:32:00 +0000878 TII->copyRegToReg(*BB, BB->end(), Reg, VRI->second,
Evan Cheng5ec4b762007-09-26 21:36:17 +0000879 SU->CopyDstRC, SU->CopySrcRC);
880 } else {
881 // Copy from physical register.
882 assert(I->Reg && "Unknown physical register!");
Chris Lattner1b989192007-12-31 04:13:23 +0000883 unsigned VRBase = RegInfo.createVirtualRegister(SU->CopyDstRC);
Evan Cheng5ec4b762007-09-26 21:36:17 +0000884 bool isNew = VRBaseMap.insert(std::make_pair(SU, VRBase));
885 assert(isNew && "Node emitted out of order - early");
Owen Anderson8f2c8932007-12-31 06:32:00 +0000886 TII->copyRegToReg(*BB, BB->end(), VRBase, I->Reg,
Evan Cheng5ec4b762007-09-26 21:36:17 +0000887 SU->CopyDstRC, SU->CopySrcRC);
888 }
889 break;
890 }
891}
892
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000893/// EmitSchedule - Emit the machine code in scheduled order.
894void ScheduleDAG::EmitSchedule() {
895 // If this is the first basic block in the function, and if it has live ins
896 // that need to be copied into vregs, emit the copies into the top of the
897 // block before emitting the code for the block.
Evan Cheng2d373922008-01-30 19:35:32 +0000898 if (&MF->front() == BB) {
Chris Lattner1b989192007-12-31 04:13:23 +0000899 for (MachineRegisterInfo::livein_iterator LI = RegInfo.livein_begin(),
900 E = RegInfo.livein_end(); LI != E; ++LI)
Evan Chengb3d91cf2007-09-26 06:25:56 +0000901 if (LI->second) {
Chris Lattner1b989192007-12-31 04:13:23 +0000902 const TargetRegisterClass *RC = RegInfo.getRegClass(LI->second);
Evan Cheng2d373922008-01-30 19:35:32 +0000903 TII->copyRegToReg(*MF->begin(), MF->begin()->end(), LI->second,
Evan Chengb3d91cf2007-09-26 06:25:56 +0000904 LI->first, RC, RC);
905 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000906 }
907
908
909 // Finally, emit the code for all of the scheduled instructions.
910 DenseMap<SDOperand, unsigned> VRBaseMap;
Evan Cheng5ec4b762007-09-26 21:36:17 +0000911 DenseMap<SUnit*, unsigned> CopyVRBaseMap;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000912 for (unsigned i = 0, e = Sequence.size(); i != e; i++) {
913 if (SUnit *SU = Sequence[i]) {
Evan Cheng93f143e2007-09-25 01:54:36 +0000914 for (unsigned j = 0, ee = SU->FlaggedNodes.size(); j != ee; ++j)
915 EmitNode(SU->FlaggedNodes[j], SU->InstanceNo, VRBaseMap);
Evan Cheng5ec4b762007-09-26 21:36:17 +0000916 if (SU->Node)
917 EmitNode(SU->Node, SU->InstanceNo, VRBaseMap);
918 else
919 EmitCrossRCCopy(SU, CopyVRBaseMap);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000920 } else {
921 // Null SUnit* is a noop.
922 EmitNoop();
923 }
924 }
925}
926
927/// dump - dump the schedule.
928void ScheduleDAG::dumpSchedule() const {
929 for (unsigned i = 0, e = Sequence.size(); i != e; i++) {
930 if (SUnit *SU = Sequence[i])
931 SU->dump(&DAG);
932 else
933 cerr << "**** NOOP ****\n";
934 }
935}
936
937
938/// Run - perform scheduling.
939///
940MachineBasicBlock *ScheduleDAG::Run() {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000941 Schedule();
942 return BB;
943}
944
945/// SUnit - Scheduling unit. It's an wrapper around either a single SDNode or
946/// a group of nodes flagged together.
947void SUnit::dump(const SelectionDAG *G) const {
948 cerr << "SU(" << NodeNum << "): ";
Evan Cheng5ec4b762007-09-26 21:36:17 +0000949 if (Node)
950 Node->dump(G);
951 else
952 cerr << "CROSS RC COPY ";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000953 cerr << "\n";
954 if (FlaggedNodes.size() != 0) {
955 for (unsigned i = 0, e = FlaggedNodes.size(); i != e; i++) {
956 cerr << " ";
957 FlaggedNodes[i]->dump(G);
958 cerr << "\n";
959 }
960 }
961}
962
963void SUnit::dumpAll(const SelectionDAG *G) const {
964 dump(G);
965
966 cerr << " # preds left : " << NumPredsLeft << "\n";
967 cerr << " # succs left : " << NumSuccsLeft << "\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000968 cerr << " Latency : " << Latency << "\n";
969 cerr << " Depth : " << Depth << "\n";
970 cerr << " Height : " << Height << "\n";
971
972 if (Preds.size() != 0) {
973 cerr << " Predecessors:\n";
974 for (SUnit::const_succ_iterator I = Preds.begin(), E = Preds.end();
975 I != E; ++I) {
Evan Chenge7959472007-09-19 01:38:40 +0000976 if (I->isCtrl)
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000977 cerr << " ch #";
978 else
979 cerr << " val #";
Evan Cheng93f143e2007-09-25 01:54:36 +0000980 cerr << I->Dep << " - SU(" << I->Dep->NodeNum << ")";
981 if (I->isSpecial)
982 cerr << " *";
983 cerr << "\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000984 }
985 }
986 if (Succs.size() != 0) {
987 cerr << " Successors:\n";
988 for (SUnit::const_succ_iterator I = Succs.begin(), E = Succs.end();
989 I != E; ++I) {
Evan Chenge7959472007-09-19 01:38:40 +0000990 if (I->isCtrl)
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000991 cerr << " ch #";
992 else
993 cerr << " val #";
Evan Cheng93f143e2007-09-25 01:54:36 +0000994 cerr << I->Dep << " - SU(" << I->Dep->NodeNum << ")";
995 if (I->isSpecial)
996 cerr << " *";
997 cerr << "\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000998 }
999 }
1000 cerr << "\n";
1001}