blob: 6197b6fa0e9f76ec66ee4afd1838d25c7cb10985 [file] [log] [blame]
Evan Chenga9c20912006-01-21 02:32:06 +00001//===---- ScheduleDAG.cpp - Implement the ScheduleDAG class ---------------===//
Chris Lattnerd32b2362005-08-18 18:45:24 +00002//
3// The LLVM Compiler Infrastructure
4//
Jim Laskey5a608dd2005-10-31 12:49:09 +00005// This file was developed by James M. Laskey and is distributed under the
Chris Lattnerd32b2362005-08-18 18:45:24 +00006// University of Illinois Open Source License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
Jim Laskeye6b90fb2005-09-26 21:57:04 +000010// This implements a simple two pass scheduler. The first pass attempts to push
11// backward any lengthy instructions and critical paths. The second pass packs
12// instructions into semi-optimal time slots.
Chris Lattnerd32b2362005-08-18 18:45:24 +000013//
14//===----------------------------------------------------------------------===//
15
Dale Johannesene7e7d0d2007-07-13 17:13:54 +000016#define DEBUG_TYPE "pre-RA-sched"
Reid Spencere5530da2007-01-12 23:31:12 +000017#include "llvm/Type.h"
Chris Lattnerb0d21ef2006-03-08 04:25:59 +000018#include "llvm/CodeGen/ScheduleDAG.h"
Chris Lattner5839bf22005-08-26 17:15:30 +000019#include "llvm/CodeGen/MachineConstantPool.h"
Chris Lattner4ccd4062005-08-19 20:45:43 +000020#include "llvm/CodeGen/MachineFunction.h"
Chris Lattner4ccd4062005-08-19 20:45:43 +000021#include "llvm/CodeGen/SSARegMap.h"
Owen Anderson07000c62006-05-12 06:33:49 +000022#include "llvm/Target/TargetData.h"
Chris Lattner2d973e42005-08-18 20:07:59 +000023#include "llvm/Target/TargetMachine.h"
24#include "llvm/Target/TargetInstrInfo.h"
Chris Lattner025c39b2005-08-26 20:54:47 +000025#include "llvm/Target/TargetLowering.h"
Evan Chenge165a782006-05-11 23:55:42 +000026#include "llvm/Support/Debug.h"
Chris Lattner54a30b92006-03-20 01:51:46 +000027#include "llvm/Support/MathExtras.h"
Chris Lattnerd32b2362005-08-18 18:45:24 +000028using namespace llvm;
29
Evan Chenga6fb1b62007-09-25 01:54:36 +000030
Evan Chenga6fb1b62007-09-25 01:54:36 +000031/// CheckForPhysRegDependency - Check if the dependency between def and use of
32/// a specified operand is a physical register dependency. If so, returns the
33/// register and the cost of copying the register.
34static void CheckForPhysRegDependency(SDNode *Def, SDNode *Use, unsigned Op,
35 const MRegisterInfo *MRI,
36 const TargetInstrInfo *TII,
37 unsigned &PhysReg, int &Cost) {
38 if (Op != 2 || Use->getOpcode() != ISD::CopyToReg)
39 return;
40
41 unsigned Reg = cast<RegisterSDNode>(Use->getOperand(1))->getReg();
42 if (MRegisterInfo::isVirtualRegister(Reg))
43 return;
44
45 unsigned ResNo = Use->getOperand(2).ResNo;
46 if (Def->isTargetOpcode()) {
47 const TargetInstrDescriptor &II = TII->get(Def->getTargetOpcode());
48 if (ResNo >= II.numDefs &&
49 II.ImplicitDefs[ResNo - II.numDefs] == Reg) {
50 PhysReg = Reg;
51 const TargetRegisterClass *RC =
Evan Cheng42d60272007-09-26 21:36:17 +000052 MRI->getPhysicalRegisterRegClass(Def->getValueType(ResNo), Reg);
Evan Chenga6fb1b62007-09-25 01:54:36 +000053 Cost = RC->getCopyCost();
54 }
55 }
56}
57
58SUnit *ScheduleDAG::Clone(SUnit *Old) {
59 SUnit *SU = NewSUnit(Old->Node);
60 for (unsigned i = 0, e = SU->FlaggedNodes.size(); i != e; ++i)
61 SU->FlaggedNodes.push_back(SU->FlaggedNodes[i]);
62 SU->InstanceNo = SUnitMap[Old->Node].size();
63 SU->Latency = Old->Latency;
64 SU->isTwoAddress = Old->isTwoAddress;
65 SU->isCommutable = Old->isCommutable;
66 SU->hasImplicitDefs = Old->hasImplicitDefs;
67 SUnitMap[Old->Node].push_back(SU);
68 return SU;
69}
70
Evan Chenge165a782006-05-11 23:55:42 +000071/// BuildSchedUnits - Build SUnits from the selection dag that we are input.
72/// This SUnit graph is similar to the SelectionDAG, but represents flagged
73/// together nodes with a single SUnit.
74void ScheduleDAG::BuildSchedUnits() {
75 // Reserve entries in the vector for each of the SUnits we are creating. This
76 // ensure that reallocation of the vector won't happen, so SUnit*'s won't get
77 // invalidated.
78 SUnits.reserve(std::distance(DAG.allnodes_begin(), DAG.allnodes_end()));
79
80 const InstrItineraryData &InstrItins = TM.getInstrItineraryData();
81
82 for (SelectionDAG::allnodes_iterator NI = DAG.allnodes_begin(),
83 E = DAG.allnodes_end(); NI != E; ++NI) {
84 if (isPassiveNode(NI)) // Leaf node, e.g. a TargetImmediate.
85 continue;
86
87 // If this node has already been processed, stop now.
Evan Chenga6fb1b62007-09-25 01:54:36 +000088 if (SUnitMap[NI].size()) continue;
Evan Chenge165a782006-05-11 23:55:42 +000089
90 SUnit *NodeSUnit = NewSUnit(NI);
91
92 // See if anything is flagged to this node, if so, add them to flagged
93 // nodes. Nodes can have at most one flag input and one flag output. Flags
94 // are required the be the last operand and result of a node.
95
96 // Scan up, adding flagged preds to FlaggedNodes.
97 SDNode *N = NI;
Evan Cheng3b97acd2006-08-07 22:12:12 +000098 if (N->getNumOperands() &&
99 N->getOperand(N->getNumOperands()-1).getValueType() == MVT::Flag) {
100 do {
101 N = N->getOperand(N->getNumOperands()-1).Val;
102 NodeSUnit->FlaggedNodes.push_back(N);
Evan Chenga6fb1b62007-09-25 01:54:36 +0000103 SUnitMap[N].push_back(NodeSUnit);
Evan Cheng3b97acd2006-08-07 22:12:12 +0000104 } while (N->getNumOperands() &&
105 N->getOperand(N->getNumOperands()-1).getValueType()== MVT::Flag);
106 std::reverse(NodeSUnit->FlaggedNodes.begin(),
107 NodeSUnit->FlaggedNodes.end());
Evan Chenge165a782006-05-11 23:55:42 +0000108 }
109
110 // Scan down, adding this node and any flagged succs to FlaggedNodes if they
111 // have a user of the flag operand.
112 N = NI;
113 while (N->getValueType(N->getNumValues()-1) == MVT::Flag) {
114 SDOperand FlagVal(N, N->getNumValues()-1);
115
116 // There are either zero or one users of the Flag result.
117 bool HasFlagUse = false;
118 for (SDNode::use_iterator UI = N->use_begin(), E = N->use_end();
119 UI != E; ++UI)
120 if (FlagVal.isOperand(*UI)) {
121 HasFlagUse = true;
122 NodeSUnit->FlaggedNodes.push_back(N);
Evan Chenga6fb1b62007-09-25 01:54:36 +0000123 SUnitMap[N].push_back(NodeSUnit);
Evan Chenge165a782006-05-11 23:55:42 +0000124 N = *UI;
125 break;
126 }
Chris Lattner228a18e2006-08-17 00:09:56 +0000127 if (!HasFlagUse) break;
Evan Chenge165a782006-05-11 23:55:42 +0000128 }
129
130 // Now all flagged nodes are in FlaggedNodes and N is the bottom-most node.
131 // Update the SUnit
132 NodeSUnit->Node = N;
Evan Chenga6fb1b62007-09-25 01:54:36 +0000133 SUnitMap[N].push_back(NodeSUnit);
Evan Chenge165a782006-05-11 23:55:42 +0000134
135 // Compute the latency for the node. We use the sum of the latencies for
136 // all nodes flagged together into this SUnit.
137 if (InstrItins.isEmpty()) {
138 // No latency information.
139 NodeSUnit->Latency = 1;
140 } else {
141 NodeSUnit->Latency = 0;
142 if (N->isTargetOpcode()) {
143 unsigned SchedClass = TII->getSchedClass(N->getTargetOpcode());
144 InstrStage *S = InstrItins.begin(SchedClass);
145 InstrStage *E = InstrItins.end(SchedClass);
146 for (; S != E; ++S)
147 NodeSUnit->Latency += S->Cycles;
148 }
149 for (unsigned i = 0, e = NodeSUnit->FlaggedNodes.size(); i != e; ++i) {
150 SDNode *FNode = NodeSUnit->FlaggedNodes[i];
151 if (FNode->isTargetOpcode()) {
152 unsigned SchedClass = TII->getSchedClass(FNode->getTargetOpcode());
153 InstrStage *S = InstrItins.begin(SchedClass);
154 InstrStage *E = InstrItins.end(SchedClass);
155 for (; S != E; ++S)
156 NodeSUnit->Latency += S->Cycles;
157 }
158 }
159 }
160 }
161
162 // Pass 2: add the preds, succs, etc.
163 for (unsigned su = 0, e = SUnits.size(); su != e; ++su) {
164 SUnit *SU = &SUnits[su];
165 SDNode *MainNode = SU->Node;
166
167 if (MainNode->isTargetOpcode()) {
168 unsigned Opc = MainNode->getTargetOpcode();
Evan Chenga6fb1b62007-09-25 01:54:36 +0000169 const TargetInstrDescriptor &TID = TII->get(Opc);
170 if (TID.ImplicitDefs)
171 SU->hasImplicitDefs = true;
172 for (unsigned i = 0; i != TID.numOperands; ++i) {
173 if (TID.getOperandConstraint(i, TOI::TIED_TO) != -1) {
Evan Cheng95f6ede2006-11-04 09:44:31 +0000174 SU->isTwoAddress = true;
175 break;
176 }
177 }
Evan Chenga6fb1b62007-09-25 01:54:36 +0000178 if (TID.Flags & M_COMMUTABLE)
Evan Cheng13d41b92006-05-12 01:58:24 +0000179 SU->isCommutable = true;
Evan Chenge165a782006-05-11 23:55:42 +0000180 }
181
182 // Find all predecessors and successors of the group.
183 // Temporarily add N to make code simpler.
184 SU->FlaggedNodes.push_back(MainNode);
185
186 for (unsigned n = 0, e = SU->FlaggedNodes.size(); n != e; ++n) {
187 SDNode *N = SU->FlaggedNodes[n];
Evan Chenga6fb1b62007-09-25 01:54:36 +0000188 if (N->isTargetOpcode() && TII->getImplicitDefs(N->getTargetOpcode()))
189 SU->hasImplicitDefs = true;
Evan Chenge165a782006-05-11 23:55:42 +0000190
191 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
192 SDNode *OpN = N->getOperand(i).Val;
193 if (isPassiveNode(OpN)) continue; // Not scheduled.
Evan Chenga6fb1b62007-09-25 01:54:36 +0000194 SUnit *OpSU = SUnitMap[OpN].front();
Evan Chenge165a782006-05-11 23:55:42 +0000195 assert(OpSU && "Node has no SUnit!");
196 if (OpSU == SU) continue; // In the same group.
197
198 MVT::ValueType OpVT = N->getOperand(i).getValueType();
199 assert(OpVT != MVT::Flag && "Flagged nodes should be in same sunit!");
200 bool isChain = OpVT == MVT::Other;
Evan Chenga6fb1b62007-09-25 01:54:36 +0000201
202 unsigned PhysReg = 0;
203 int Cost = 1;
204 // Determine if this is a physical register dependency.
205 CheckForPhysRegDependency(OpN, N, i, MRI, TII, PhysReg, Cost);
206 SU->addPred(OpSU, isChain, false, PhysReg, Cost);
Evan Chenge165a782006-05-11 23:55:42 +0000207 }
208 }
209
210 // Remove MainNode from FlaggedNodes again.
211 SU->FlaggedNodes.pop_back();
212 }
213
214 return;
215}
216
Evan Chenge165a782006-05-11 23:55:42 +0000217void ScheduleDAG::CalculateDepths() {
Evan Cheng99126282007-07-06 01:37:28 +0000218 std::vector<std::pair<SUnit*, unsigned> > WorkList;
Evan Chenge165a782006-05-11 23:55:42 +0000219 for (unsigned i = 0, e = SUnits.size(); i != e; ++i)
Evan Cheng69001322007-09-12 23:45:46 +0000220 if (SUnits[i].Preds.size() == 0)
Evan Cheng99126282007-07-06 01:37:28 +0000221 WorkList.push_back(std::make_pair(&SUnits[i], 0U));
Evan Chenge165a782006-05-11 23:55:42 +0000222
Evan Cheng99126282007-07-06 01:37:28 +0000223 while (!WorkList.empty()) {
224 SUnit *SU = WorkList.back().first;
225 unsigned Depth = WorkList.back().second;
226 WorkList.pop_back();
227 if (SU->Depth == 0 || Depth > SU->Depth) {
228 SU->Depth = Depth;
229 for (SUnit::succ_iterator I = SU->Succs.begin(), E = SU->Succs.end();
230 I != E; ++I)
Evan Cheng713a98d2007-09-19 01:38:40 +0000231 WorkList.push_back(std::make_pair(I->Dep, Depth+1));
Evan Cheng99126282007-07-06 01:37:28 +0000232 }
Evan Cheng626da3d2006-05-12 06:05:18 +0000233 }
Evan Chenge165a782006-05-11 23:55:42 +0000234}
Evan Cheng99126282007-07-06 01:37:28 +0000235
Evan Chenge165a782006-05-11 23:55:42 +0000236void ScheduleDAG::CalculateHeights() {
Evan Cheng99126282007-07-06 01:37:28 +0000237 std::vector<std::pair<SUnit*, unsigned> > WorkList;
Evan Chenga6fb1b62007-09-25 01:54:36 +0000238 SUnit *Root = SUnitMap[DAG.getRoot().Val].front();
Evan Cheng99126282007-07-06 01:37:28 +0000239 WorkList.push_back(std::make_pair(Root, 0U));
240
241 while (!WorkList.empty()) {
242 SUnit *SU = WorkList.back().first;
243 unsigned Height = WorkList.back().second;
244 WorkList.pop_back();
245 if (SU->Height == 0 || Height > SU->Height) {
246 SU->Height = Height;
247 for (SUnit::pred_iterator I = SU->Preds.begin(), E = SU->Preds.end();
248 I != E; ++I)
Evan Cheng713a98d2007-09-19 01:38:40 +0000249 WorkList.push_back(std::make_pair(I->Dep, Height+1));
Evan Cheng99126282007-07-06 01:37:28 +0000250 }
251 }
Evan Chenge165a782006-05-11 23:55:42 +0000252}
253
Jim Laskeye6b90fb2005-09-26 21:57:04 +0000254/// CountResults - The results of target nodes have register or immediate
255/// operands first, then an optional chain, and optional flag operands (which do
256/// not go into the machine instrs.)
Evan Cheng95f6ede2006-11-04 09:44:31 +0000257unsigned ScheduleDAG::CountResults(SDNode *Node) {
Jim Laskeyb6d4c2c2005-09-30 19:15:27 +0000258 unsigned N = Node->getNumValues();
259 while (N && Node->getValueType(N - 1) == MVT::Flag)
Jim Laskeye6b90fb2005-09-26 21:57:04 +0000260 --N;
Jim Laskeyb6d4c2c2005-09-30 19:15:27 +0000261 if (N && Node->getValueType(N - 1) == MVT::Other)
Jim Laskeye6b90fb2005-09-26 21:57:04 +0000262 --N; // Skip over chain result.
263 return N;
264}
265
266/// CountOperands The inputs to target nodes have any actual inputs first,
267/// followed by an optional chain operand, then flag operands. Compute the
268/// number of actual operands that will go into the machine instr.
Evan Cheng95f6ede2006-11-04 09:44:31 +0000269unsigned ScheduleDAG::CountOperands(SDNode *Node) {
Jim Laskeyb6d4c2c2005-09-30 19:15:27 +0000270 unsigned N = Node->getNumOperands();
271 while (N && Node->getOperand(N - 1).getValueType() == MVT::Flag)
Jim Laskeye6b90fb2005-09-26 21:57:04 +0000272 --N;
Jim Laskeyb6d4c2c2005-09-30 19:15:27 +0000273 if (N && Node->getOperand(N - 1).getValueType() == MVT::Other)
Jim Laskeye6b90fb2005-09-26 21:57:04 +0000274 --N; // Ignore chain if it exists.
275 return N;
276}
277
Jim Laskey60f09922006-07-21 20:57:35 +0000278static const TargetRegisterClass *getInstrOperandRegClass(
279 const MRegisterInfo *MRI,
280 const TargetInstrInfo *TII,
281 const TargetInstrDescriptor *II,
282 unsigned Op) {
283 if (Op >= II->numOperands) {
284 assert((II->Flags & M_VARIABLE_OPS)&& "Invalid operand # of instruction");
285 return NULL;
286 }
287 const TargetOperandInfo &toi = II->OpInfo[Op];
288 return (toi.Flags & M_LOOK_UP_PTR_REG_CLASS)
289 ? TII->getPointerRegClass() : MRI->getRegClass(toi.RegClass);
290}
291
Evan Chenga6fb1b62007-09-25 01:54:36 +0000292void ScheduleDAG::EmitCopyFromReg(SDNode *Node, unsigned ResNo,
293 unsigned InstanceNo, unsigned SrcReg,
Evan Cheng84097472007-08-02 00:28:15 +0000294 DenseMap<SDOperand, unsigned> &VRBaseMap) {
295 unsigned VRBase = 0;
296 if (MRegisterInfo::isVirtualRegister(SrcReg)) {
297 // Just use the input register directly!
Evan Chenga6fb1b62007-09-25 01:54:36 +0000298 if (InstanceNo > 0)
299 VRBaseMap.erase(SDOperand(Node, ResNo));
Evan Cheng84097472007-08-02 00:28:15 +0000300 bool isNew = VRBaseMap.insert(std::make_pair(SDOperand(Node,ResNo),SrcReg));
301 assert(isNew && "Node emitted out of order - early");
302 return;
303 }
304
305 // If the node is only used by a CopyToReg and the dest reg is a vreg, use
306 // the CopyToReg'd destination register instead of creating a new vreg.
Evan Chenga6fb1b62007-09-25 01:54:36 +0000307 bool MatchReg = true;
Evan Cheng84097472007-08-02 00:28:15 +0000308 for (SDNode::use_iterator UI = Node->use_begin(), E = Node->use_end();
309 UI != E; ++UI) {
310 SDNode *Use = *UI;
Evan Chenga6fb1b62007-09-25 01:54:36 +0000311 bool Match = true;
Evan Cheng84097472007-08-02 00:28:15 +0000312 if (Use->getOpcode() == ISD::CopyToReg &&
313 Use->getOperand(2).Val == Node &&
314 Use->getOperand(2).ResNo == ResNo) {
315 unsigned DestReg = cast<RegisterSDNode>(Use->getOperand(1))->getReg();
316 if (MRegisterInfo::isVirtualRegister(DestReg)) {
317 VRBase = DestReg;
Evan Chenga6fb1b62007-09-25 01:54:36 +0000318 Match = false;
319 } else if (DestReg != SrcReg)
320 Match = false;
321 } else {
322 for (unsigned i = 0, e = Use->getNumOperands(); i != e; ++i) {
323 SDOperand Op = Use->getOperand(i);
324 if (Op.Val != Node)
325 continue;
326 MVT::ValueType VT = Node->getValueType(Op.ResNo);
327 if (VT != MVT::Other && VT != MVT::Flag)
328 Match = false;
Evan Cheng84097472007-08-02 00:28:15 +0000329 }
330 }
Evan Chenga6fb1b62007-09-25 01:54:36 +0000331 MatchReg &= Match;
332 if (VRBase)
333 break;
Evan Cheng84097472007-08-02 00:28:15 +0000334 }
335
Evan Cheng84097472007-08-02 00:28:15 +0000336 const TargetRegisterClass *TRC = 0;
Evan Chenga6fb1b62007-09-25 01:54:36 +0000337 // Figure out the register class to create for the destreg.
338 if (VRBase)
Evan Cheng84097472007-08-02 00:28:15 +0000339 TRC = RegMap->getRegClass(VRBase);
Evan Chenga6fb1b62007-09-25 01:54:36 +0000340 else
Evan Cheng42d60272007-09-26 21:36:17 +0000341 TRC = MRI->getPhysicalRegisterRegClass(Node->getValueType(ResNo), SrcReg);
Evan Chenga6fb1b62007-09-25 01:54:36 +0000342
343 // If all uses are reading from the src physical register and copying the
344 // register is either impossible or very expensive, then don't create a copy.
345 if (MatchReg && TRC->getCopyCost() < 0) {
346 VRBase = SrcReg;
347 } else {
Evan Cheng84097472007-08-02 00:28:15 +0000348 // Create the reg, emit the copy.
349 VRBase = RegMap->createVirtualRegister(TRC);
Evan Cheng9efce632007-09-26 06:25:56 +0000350 MRI->copyRegToReg(*BB, BB->end(), VRBase, SrcReg, TRC, TRC);
Evan Cheng84097472007-08-02 00:28:15 +0000351 }
Evan Cheng84097472007-08-02 00:28:15 +0000352
Evan Chenga6fb1b62007-09-25 01:54:36 +0000353 if (InstanceNo > 0)
354 VRBaseMap.erase(SDOperand(Node, ResNo));
Evan Cheng84097472007-08-02 00:28:15 +0000355 bool isNew = VRBaseMap.insert(std::make_pair(SDOperand(Node,ResNo), VRBase));
356 assert(isNew && "Node emitted out of order - early");
357}
358
359void ScheduleDAG::CreateVirtualRegisters(SDNode *Node,
360 MachineInstr *MI,
361 const TargetInstrDescriptor &II,
362 DenseMap<SDOperand, unsigned> &VRBaseMap) {
363 for (unsigned i = 0; i < II.numDefs; ++i) {
Evan Chengaf825c82007-07-10 07:08:32 +0000364 // If the specific node value is only used by a CopyToReg and the dest reg
365 // is a vreg, use the CopyToReg'd destination register instead of creating
366 // a new vreg.
367 unsigned VRBase = 0;
368 for (SDNode::use_iterator UI = Node->use_begin(), E = Node->use_end();
369 UI != E; ++UI) {
370 SDNode *Use = *UI;
371 if (Use->getOpcode() == ISD::CopyToReg &&
372 Use->getOperand(2).Val == Node &&
373 Use->getOperand(2).ResNo == i) {
374 unsigned Reg = cast<RegisterSDNode>(Use->getOperand(1))->getReg();
375 if (MRegisterInfo::isVirtualRegister(Reg)) {
376 VRBase = Reg;
377 MI->addRegOperand(Reg, true);
378 break;
379 }
380 }
381 }
382
Evan Cheng84097472007-08-02 00:28:15 +0000383 // Create the result registers for this node and add the result regs to
384 // the machine instruction.
Evan Chengaf825c82007-07-10 07:08:32 +0000385 if (VRBase == 0) {
Evan Chengaf825c82007-07-10 07:08:32 +0000386 const TargetRegisterClass *RC = getInstrOperandRegClass(MRI, TII, &II, i);
387 assert(RC && "Isn't a register operand!");
388 VRBase = RegMap->createVirtualRegister(RC);
389 MI->addRegOperand(VRBase, true);
390 }
391
392 bool isNew = VRBaseMap.insert(std::make_pair(SDOperand(Node,i), VRBase));
393 assert(isNew && "Node emitted out of order - early");
Jim Laskeye6b90fb2005-09-26 21:57:04 +0000394 }
Jim Laskeye6b90fb2005-09-26 21:57:04 +0000395}
396
Chris Lattnerdf375062006-03-10 07:25:12 +0000397/// getVR - Return the virtual register corresponding to the specified result
398/// of the specified node.
Evan Chengaf825c82007-07-10 07:08:32 +0000399static unsigned getVR(SDOperand Op, DenseMap<SDOperand, unsigned> &VRBaseMap) {
400 DenseMap<SDOperand, unsigned>::iterator I = VRBaseMap.find(Op);
Chris Lattnerdf375062006-03-10 07:25:12 +0000401 assert(I != VRBaseMap.end() && "Node emitted out of order - late");
Evan Chengaf825c82007-07-10 07:08:32 +0000402 return I->second;
Chris Lattnerdf375062006-03-10 07:25:12 +0000403}
404
405
Chris Lattnered18b682006-02-24 18:54:03 +0000406/// AddOperand - Add the specified operand to the specified machine instr. II
407/// specifies the instruction information for the node, and IIOpNum is the
408/// operand number (in the II) that we are adding. IIOpNum and II are used for
409/// assertions only.
410void ScheduleDAG::AddOperand(MachineInstr *MI, SDOperand Op,
411 unsigned IIOpNum,
Chris Lattnerdf375062006-03-10 07:25:12 +0000412 const TargetInstrDescriptor *II,
Evan Chengaf825c82007-07-10 07:08:32 +0000413 DenseMap<SDOperand, unsigned> &VRBaseMap) {
Chris Lattnered18b682006-02-24 18:54:03 +0000414 if (Op.isTargetOpcode()) {
415 // Note that this case is redundant with the final else block, but we
416 // include it because it is the most common and it makes the logic
417 // simpler here.
418 assert(Op.getValueType() != MVT::Other &&
419 Op.getValueType() != MVT::Flag &&
420 "Chain and flag operands should occur at end of operand list!");
421
422 // Get/emit the operand.
Chris Lattnerdf375062006-03-10 07:25:12 +0000423 unsigned VReg = getVR(Op, VRBaseMap);
Evan Cheng5e2456c2007-07-10 17:52:20 +0000424 const TargetInstrDescriptor *TID = MI->getInstrDescriptor();
425 bool isOptDef = (IIOpNum < TID->numOperands)
426 ? (TID->OpInfo[IIOpNum].Flags & M_OPTIONAL_DEF_OPERAND) : false;
427 MI->addRegOperand(VReg, isOptDef);
Chris Lattnered18b682006-02-24 18:54:03 +0000428
429 // Verify that it is right.
430 assert(MRegisterInfo::isVirtualRegister(VReg) && "Not a vreg?");
431 if (II) {
Jim Laskey60f09922006-07-21 20:57:35 +0000432 const TargetRegisterClass *RC =
433 getInstrOperandRegClass(MRI, TII, II, IIOpNum);
Evan Cheng21d03f22006-05-18 20:42:07 +0000434 assert(RC && "Don't have operand info for this instruction!");
Chris Lattner01528292007-02-15 18:17:56 +0000435 const TargetRegisterClass *VRC = RegMap->getRegClass(VReg);
436 if (VRC != RC) {
437 cerr << "Register class of operand and regclass of use don't agree!\n";
438#ifndef NDEBUG
439 cerr << "Operand = " << IIOpNum << "\n";
Chris Lattner95ad9432007-02-17 06:38:37 +0000440 cerr << "Op->Val = "; Op.Val->dump(&DAG); cerr << "\n";
Chris Lattner01528292007-02-15 18:17:56 +0000441 cerr << "MI = "; MI->print(cerr);
442 cerr << "VReg = " << VReg << "\n";
443 cerr << "VReg RegClass size = " << VRC->getSize()
Chris Lattner5d4a9f72007-02-15 18:19:15 +0000444 << ", align = " << VRC->getAlignment() << "\n";
Chris Lattner01528292007-02-15 18:17:56 +0000445 cerr << "Expected RegClass size = " << RC->getSize()
Chris Lattner5d4a9f72007-02-15 18:19:15 +0000446 << ", align = " << RC->getAlignment() << "\n";
Chris Lattner01528292007-02-15 18:17:56 +0000447#endif
448 cerr << "Fatal error, aborting.\n";
449 abort();
450 }
Chris Lattnered18b682006-02-24 18:54:03 +0000451 }
452 } else if (ConstantSDNode *C =
453 dyn_cast<ConstantSDNode>(Op)) {
Chris Lattner2d90ac72006-05-04 18:05:43 +0000454 MI->addImmOperand(C->getValue());
Evan Cheng489a87c2007-01-05 20:59:06 +0000455 } else if (RegisterSDNode *R =
Chris Lattnered18b682006-02-24 18:54:03 +0000456 dyn_cast<RegisterSDNode>(Op)) {
Chris Lattner09e46062006-09-05 02:31:13 +0000457 MI->addRegOperand(R->getReg(), false);
Chris Lattnered18b682006-02-24 18:54:03 +0000458 } else if (GlobalAddressSDNode *TGA =
459 dyn_cast<GlobalAddressSDNode>(Op)) {
Chris Lattnerea50fab2006-05-04 01:15:02 +0000460 MI->addGlobalAddressOperand(TGA->getGlobal(), TGA->getOffset());
Chris Lattnered18b682006-02-24 18:54:03 +0000461 } else if (BasicBlockSDNode *BB =
462 dyn_cast<BasicBlockSDNode>(Op)) {
463 MI->addMachineBasicBlockOperand(BB->getBasicBlock());
464 } else if (FrameIndexSDNode *FI =
465 dyn_cast<FrameIndexSDNode>(Op)) {
466 MI->addFrameIndexOperand(FI->getIndex());
Nate Begeman37efe672006-04-22 18:53:45 +0000467 } else if (JumpTableSDNode *JT =
468 dyn_cast<JumpTableSDNode>(Op)) {
469 MI->addJumpTableIndexOperand(JT->getIndex());
Chris Lattnered18b682006-02-24 18:54:03 +0000470 } else if (ConstantPoolSDNode *CP =
471 dyn_cast<ConstantPoolSDNode>(Op)) {
Evan Cheng404cb4f2006-02-25 09:54:52 +0000472 int Offset = CP->getOffset();
Chris Lattnered18b682006-02-24 18:54:03 +0000473 unsigned Align = CP->getAlignment();
Evan Chengd6594ae2006-09-12 21:00:35 +0000474 const Type *Type = CP->getType();
Chris Lattnered18b682006-02-24 18:54:03 +0000475 // MachineConstantPool wants an explicit alignment.
476 if (Align == 0) {
Evan Chengde268f72007-01-24 07:03:39 +0000477 Align = TM.getTargetData()->getPreferredTypeAlignmentShift(Type);
Evan Chengf6d039a2007-01-22 23:13:55 +0000478 if (Align == 0) {
Reid Spencerac9dcb92007-02-15 03:39:18 +0000479 // Alignment of vector types. FIXME!
Evan Chengf6d039a2007-01-22 23:13:55 +0000480 Align = TM.getTargetData()->getTypeSize(Type);
481 Align = Log2_64(Align);
Chris Lattner54a30b92006-03-20 01:51:46 +0000482 }
Chris Lattnered18b682006-02-24 18:54:03 +0000483 }
484
Evan Chengd6594ae2006-09-12 21:00:35 +0000485 unsigned Idx;
486 if (CP->isMachineConstantPoolEntry())
487 Idx = ConstPool->getConstantPoolIndex(CP->getMachineCPVal(), Align);
488 else
489 Idx = ConstPool->getConstantPoolIndex(CP->getConstVal(), Align);
Evan Cheng404cb4f2006-02-25 09:54:52 +0000490 MI->addConstantPoolIndexOperand(Idx, Offset);
Chris Lattnered18b682006-02-24 18:54:03 +0000491 } else if (ExternalSymbolSDNode *ES =
492 dyn_cast<ExternalSymbolSDNode>(Op)) {
Chris Lattnerea50fab2006-05-04 01:15:02 +0000493 MI->addExternalSymbolOperand(ES->getSymbol());
Chris Lattnered18b682006-02-24 18:54:03 +0000494 } else {
495 assert(Op.getValueType() != MVT::Other &&
496 Op.getValueType() != MVT::Flag &&
497 "Chain and flag operands should occur at end of operand list!");
Chris Lattnerdf375062006-03-10 07:25:12 +0000498 unsigned VReg = getVR(Op, VRBaseMap);
Chris Lattner09e46062006-09-05 02:31:13 +0000499 MI->addRegOperand(VReg, false);
Chris Lattnered18b682006-02-24 18:54:03 +0000500
501 // Verify that it is right.
502 assert(MRegisterInfo::isVirtualRegister(VReg) && "Not a vreg?");
503 if (II) {
Jim Laskey60f09922006-07-21 20:57:35 +0000504 const TargetRegisterClass *RC =
505 getInstrOperandRegClass(MRI, TII, II, IIOpNum);
Evan Cheng21d03f22006-05-18 20:42:07 +0000506 assert(RC && "Don't have operand info for this instruction!");
507 assert(RegMap->getRegClass(VReg) == RC &&
Chris Lattnered18b682006-02-24 18:54:03 +0000508 "Register class of operand and regclass of use don't agree!");
509 }
510 }
511
512}
513
Christopher Lambe24f8f12007-07-26 08:12:07 +0000514// Returns the Register Class of a subregister
515static const TargetRegisterClass *getSubRegisterRegClass(
516 const TargetRegisterClass *TRC,
517 unsigned SubIdx) {
518 // Pick the register class of the subregister
519 MRegisterInfo::regclass_iterator I = TRC->subregclasses_begin() + SubIdx-1;
520 assert(I < TRC->subregclasses_end() &&
521 "Invalid subregister index for register class");
522 return *I;
523}
524
525static const TargetRegisterClass *getSuperregRegisterClass(
526 const TargetRegisterClass *TRC,
527 unsigned SubIdx,
528 MVT::ValueType VT) {
529 // Pick the register class of the superegister for this type
530 for (MRegisterInfo::regclass_iterator I = TRC->superregclasses_begin(),
531 E = TRC->superregclasses_end(); I != E; ++I)
532 if ((*I)->hasType(VT) && getSubRegisterRegClass(*I, SubIdx) == TRC)
533 return *I;
534 assert(false && "Couldn't find the register class");
535 return 0;
536}
537
538/// EmitSubregNode - Generate machine code for subreg nodes.
539///
540void ScheduleDAG::EmitSubregNode(SDNode *Node,
541 DenseMap<SDOperand, unsigned> &VRBaseMap) {
542 unsigned VRBase = 0;
543 unsigned Opc = Node->getTargetOpcode();
544 if (Opc == TargetInstrInfo::EXTRACT_SUBREG) {
545 // If the node is only used by a CopyToReg and the dest reg is a vreg, use
546 // the CopyToReg'd destination register instead of creating a new vreg.
547 for (SDNode::use_iterator UI = Node->use_begin(), E = Node->use_end();
548 UI != E; ++UI) {
549 SDNode *Use = *UI;
550 if (Use->getOpcode() == ISD::CopyToReg &&
551 Use->getOperand(2).Val == Node) {
552 unsigned DestReg = cast<RegisterSDNode>(Use->getOperand(1))->getReg();
553 if (MRegisterInfo::isVirtualRegister(DestReg)) {
554 VRBase = DestReg;
555 break;
556 }
557 }
558 }
559
560 unsigned SubIdx = cast<ConstantSDNode>(Node->getOperand(1))->getValue();
561
562 // TODO: If the node is a use of a CopyFromReg from a physical register
563 // fold the extract into the copy now
564
565 // TODO: Add tracking info to SSARegMap of which vregs are subregs
566 // to allow coalescing in the allocator
567
568 // Create the extract_subreg machine instruction.
569 MachineInstr *MI =
570 new MachineInstr(BB, TII->get(TargetInstrInfo::EXTRACT_SUBREG));
571
572 // Figure out the register class to create for the destreg.
573 unsigned VReg = getVR(Node->getOperand(0), VRBaseMap);
574 const TargetRegisterClass *TRC = RegMap->getRegClass(VReg);
575 const TargetRegisterClass *SRC = getSubRegisterRegClass(TRC, SubIdx);
576
577 if (VRBase) {
578 // Grab the destination register
579 const TargetRegisterClass *DRC = 0;
580 DRC = RegMap->getRegClass(VRBase);
581 assert(SRC == DRC &&
582 "Source subregister and destination must have the same class");
583 } else {
584 // Create the reg
585 VRBase = RegMap->createVirtualRegister(SRC);
586 }
587
588 // Add def, source, and subreg index
589 MI->addRegOperand(VRBase, true);
590 AddOperand(MI, Node->getOperand(0), 0, 0, VRBaseMap);
591 MI->addImmOperand(SubIdx);
592
593 } else if (Opc == TargetInstrInfo::INSERT_SUBREG) {
594 assert((Node->getNumOperands() == 2 || Node->getNumOperands() == 3) &&
595 "Malformed insert_subreg node");
596 bool isUndefInput = (Node->getNumOperands() == 2);
597 unsigned SubReg = 0;
598 unsigned SubIdx = 0;
599
600 if (isUndefInput) {
601 SubReg = getVR(Node->getOperand(0), VRBaseMap);
602 SubIdx = cast<ConstantSDNode>(Node->getOperand(1))->getValue();
603 } else {
604 SubReg = getVR(Node->getOperand(1), VRBaseMap);
605 SubIdx = cast<ConstantSDNode>(Node->getOperand(2))->getValue();
606 }
607
608 // TODO: Add tracking info to SSARegMap of which vregs are subregs
609 // to allow coalescing in the allocator
610
611 // If the node is only used by a CopyToReg and the dest reg is a vreg, use
612 // the CopyToReg'd destination register instead of creating a new vreg.
613 // If the CopyToReg'd destination register is physical, then fold the
614 // insert into the copy
615 for (SDNode::use_iterator UI = Node->use_begin(), E = Node->use_end();
616 UI != E; ++UI) {
617 SDNode *Use = *UI;
618 if (Use->getOpcode() == ISD::CopyToReg &&
619 Use->getOperand(2).Val == Node) {
620 unsigned DestReg = cast<RegisterSDNode>(Use->getOperand(1))->getReg();
621 if (MRegisterInfo::isVirtualRegister(DestReg)) {
622 VRBase = DestReg;
623 break;
624 }
625 }
626 }
627
628 // Create the insert_subreg machine instruction.
629 MachineInstr *MI =
630 new MachineInstr(BB, TII->get(TargetInstrInfo::INSERT_SUBREG));
631
632 // Figure out the register class to create for the destreg.
633 const TargetRegisterClass *TRC = 0;
634 if (VRBase) {
635 TRC = RegMap->getRegClass(VRBase);
636 } else {
637 TRC = getSuperregRegisterClass(RegMap->getRegClass(SubReg),
638 SubIdx,
639 Node->getValueType(0));
640 assert(TRC && "Couldn't determine register class for insert_subreg");
641 VRBase = RegMap->createVirtualRegister(TRC); // Create the reg
642 }
643
644 MI->addRegOperand(VRBase, true);
645 AddOperand(MI, Node->getOperand(0), 0, 0, VRBaseMap);
646 if (!isUndefInput)
647 AddOperand(MI, Node->getOperand(1), 0, 0, VRBaseMap);
648 MI->addImmOperand(SubIdx);
649 } else
650 assert(0 && "Node is not a subreg insert or extract");
651
652 bool isNew = VRBaseMap.insert(std::make_pair(SDOperand(Node,0), VRBase));
653 assert(isNew && "Node emitted out of order - early");
654}
655
Jim Laskeyb6d4c2c2005-09-30 19:15:27 +0000656/// EmitNode - Generate machine code for an node and needed dependencies.
Jim Laskeye6b90fb2005-09-26 21:57:04 +0000657///
Evan Chenga6fb1b62007-09-25 01:54:36 +0000658void ScheduleDAG::EmitNode(SDNode *Node, unsigned InstanceNo,
Evan Chengaf825c82007-07-10 07:08:32 +0000659 DenseMap<SDOperand, unsigned> &VRBaseMap) {
Jim Laskeyb6d4c2c2005-09-30 19:15:27 +0000660 // If machine instruction
661 if (Node->isTargetOpcode()) {
662 unsigned Opc = Node->getTargetOpcode();
Christopher Lambe24f8f12007-07-26 08:12:07 +0000663
664 // Handle subreg insert/extract specially
665 if (Opc == TargetInstrInfo::EXTRACT_SUBREG ||
666 Opc == TargetInstrInfo::INSERT_SUBREG) {
667 EmitSubregNode(Node, VRBaseMap);
668 return;
669 }
670
Evan Chenga9c20912006-01-21 02:32:06 +0000671 const TargetInstrDescriptor &II = TII->get(Opc);
Chris Lattner2d973e42005-08-18 20:07:59 +0000672
Jim Laskeyb6d4c2c2005-09-30 19:15:27 +0000673 unsigned NumResults = CountResults(Node);
674 unsigned NodeOperands = CountOperands(Node);
Jim Laskeye6b90fb2005-09-26 21:57:04 +0000675 unsigned NumMIOperands = NodeOperands + NumResults;
Evan Cheng84097472007-08-02 00:28:15 +0000676 bool HasPhysRegOuts = (NumResults > II.numDefs) && II.ImplicitDefs;
Chris Lattnerda8abb02005-09-01 18:44:10 +0000677#ifndef NDEBUG
Evan Cheng8d3af5e2006-06-15 07:22:16 +0000678 assert((unsigned(II.numOperands) == NumMIOperands ||
Evan Cheng84097472007-08-02 00:28:15 +0000679 HasPhysRegOuts || (II.Flags & M_VARIABLE_OPS)) &&
Chris Lattner2d973e42005-08-18 20:07:59 +0000680 "#operands for dag node doesn't match .td file!");
Chris Lattnerca6aa2f2005-08-19 01:01:34 +0000681#endif
Chris Lattner2d973e42005-08-18 20:07:59 +0000682
683 // Create the new machine instruction.
Evan Chengc0f64ff2006-11-27 23:37:22 +0000684 MachineInstr *MI = new MachineInstr(II);
Chris Lattner2d973e42005-08-18 20:07:59 +0000685
686 // Add result register values for things that are defined by this
687 // instruction.
Evan Chengaf825c82007-07-10 07:08:32 +0000688 if (NumResults)
Evan Cheng84097472007-08-02 00:28:15 +0000689 CreateVirtualRegisters(Node, MI, II, VRBaseMap);
Jim Laskeyb6d4c2c2005-09-30 19:15:27 +0000690
691 // Emit all of the actual operands of this instruction, adding them to the
692 // instruction as appropriate.
Chris Lattnered18b682006-02-24 18:54:03 +0000693 for (unsigned i = 0; i != NodeOperands; ++i)
Evan Cheng84097472007-08-02 00:28:15 +0000694 AddOperand(MI, Node->getOperand(i), i+II.numDefs, &II, VRBaseMap);
Evan Cheng13d41b92006-05-12 01:58:24 +0000695
696 // Commute node if it has been determined to be profitable.
697 if (CommuteSet.count(Node)) {
698 MachineInstr *NewMI = TII->commuteInstruction(MI);
699 if (NewMI == 0)
Bill Wendling832171c2006-12-07 20:04:42 +0000700 DOUT << "Sched: COMMUTING FAILED!\n";
Evan Cheng13d41b92006-05-12 01:58:24 +0000701 else {
Bill Wendling832171c2006-12-07 20:04:42 +0000702 DOUT << "Sched: COMMUTED TO: " << *NewMI;
Evan Cheng4c6f2f92006-05-31 18:03:39 +0000703 if (MI != NewMI) {
704 delete MI;
705 MI = NewMI;
706 }
Evan Cheng13d41b92006-05-12 01:58:24 +0000707 }
708 }
709
Jim Laskeyb6d4c2c2005-09-30 19:15:27 +0000710 // Now that we have emitted all operands, emit this instruction itself.
711 if ((II.Flags & M_USES_CUSTOM_DAG_SCHED_INSERTION) == 0) {
712 BB->insert(BB->end(), MI);
713 } else {
714 // Insert this instruction into the end of the basic block, potentially
715 // taking some custom action.
716 BB = DAG.getTargetLoweringInfo().InsertAtEndOfBasicBlock(MI, BB);
717 }
Evan Cheng84097472007-08-02 00:28:15 +0000718
719 // Additional results must be an physical register def.
720 if (HasPhysRegOuts) {
721 for (unsigned i = II.numDefs; i < NumResults; ++i) {
722 unsigned Reg = II.ImplicitDefs[i - II.numDefs];
Evan Cheng33d55952007-08-02 05:29:38 +0000723 if (Node->hasAnyUseOfValue(i))
Evan Chenga6fb1b62007-09-25 01:54:36 +0000724 EmitCopyFromReg(Node, i, InstanceNo, Reg, VRBaseMap);
Evan Cheng84097472007-08-02 00:28:15 +0000725 }
726 }
Jim Laskeyb6d4c2c2005-09-30 19:15:27 +0000727 } else {
728 switch (Node->getOpcode()) {
729 default:
Jim Laskey16d42c62006-07-11 18:25:13 +0000730#ifndef NDEBUG
Dan Gohmanb5bec2b2007-06-19 14:13:56 +0000731 Node->dump(&DAG);
Jim Laskey16d42c62006-07-11 18:25:13 +0000732#endif
Jim Laskeyb6d4c2c2005-09-30 19:15:27 +0000733 assert(0 && "This target-independent node should have been selected!");
734 case ISD::EntryToken: // fall thru
735 case ISD::TokenFactor:
Jim Laskey1ee29252007-01-26 14:34:52 +0000736 case ISD::LABEL:
Jim Laskeyb6d4c2c2005-09-30 19:15:27 +0000737 break;
738 case ISD::CopyToReg: {
Evan Cheng489a87c2007-01-05 20:59:06 +0000739 unsigned InReg;
740 if (RegisterSDNode *R = dyn_cast<RegisterSDNode>(Node->getOperand(2)))
741 InReg = R->getReg();
742 else
743 InReg = getVR(Node->getOperand(2), VRBaseMap);
Chris Lattnera4176522005-10-30 18:54:27 +0000744 unsigned DestReg = cast<RegisterSDNode>(Node->getOperand(1))->getReg();
Lauro Ramos Venancio8334b9f2007-03-20 16:46:44 +0000745 if (InReg != DestReg) {// Coalesced away the copy?
746 const TargetRegisterClass *TRC = 0;
747 // Get the target register class
Lauro Ramos Venancioa0a26b72007-03-20 20:09:03 +0000748 if (MRegisterInfo::isVirtualRegister(InReg))
Lauro Ramos Venancio8334b9f2007-03-20 16:46:44 +0000749 TRC = RegMap->getRegClass(InReg);
Lauro Ramos Venancioa0a26b72007-03-20 20:09:03 +0000750 else
Evan Cheng42d60272007-09-26 21:36:17 +0000751 TRC =
752 MRI->getPhysicalRegisterRegClass(Node->getOperand(2).getValueType(),
Lauro Ramos Venancioa0a26b72007-03-20 20:09:03 +0000753 InReg);
Evan Cheng9efce632007-09-26 06:25:56 +0000754 MRI->copyRegToReg(*BB, BB->end(), DestReg, InReg, TRC, TRC);
Lauro Ramos Venancio8334b9f2007-03-20 16:46:44 +0000755 }
Jim Laskeyb6d4c2c2005-09-30 19:15:27 +0000756 break;
757 }
758 case ISD::CopyFromReg: {
759 unsigned SrcReg = cast<RegisterSDNode>(Node->getOperand(1))->getReg();
Evan Chenga6fb1b62007-09-25 01:54:36 +0000760 EmitCopyFromReg(Node, 0, InstanceNo, SrcReg, VRBaseMap);
Jim Laskeyb6d4c2c2005-09-30 19:15:27 +0000761 break;
762 }
Chris Lattneracc43bf2006-01-26 23:28:04 +0000763 case ISD::INLINEASM: {
764 unsigned NumOps = Node->getNumOperands();
765 if (Node->getOperand(NumOps-1).getValueType() == MVT::Flag)
766 --NumOps; // Ignore the flag operand.
767
768 // Create the inline asm machine instruction.
769 MachineInstr *MI =
Evan Chengc0f64ff2006-11-27 23:37:22 +0000770 new MachineInstr(BB, TII->get(TargetInstrInfo::INLINEASM));
Chris Lattneracc43bf2006-01-26 23:28:04 +0000771
772 // Add the asm string as an external symbol operand.
773 const char *AsmStr =
774 cast<ExternalSymbolSDNode>(Node->getOperand(1))->getSymbol();
Chris Lattnerea50fab2006-05-04 01:15:02 +0000775 MI->addExternalSymbolOperand(AsmStr);
Chris Lattneracc43bf2006-01-26 23:28:04 +0000776
777 // Add all of the operand registers to the instruction.
Chris Lattnerc3a9f8d2006-02-23 19:21:04 +0000778 for (unsigned i = 2; i != NumOps;) {
779 unsigned Flags = cast<ConstantSDNode>(Node->getOperand(i))->getValue();
Chris Lattnerfd6d2822006-02-24 19:18:20 +0000780 unsigned NumVals = Flags >> 3;
Chris Lattnerc3a9f8d2006-02-23 19:21:04 +0000781
Chris Lattner2d90ac72006-05-04 18:05:43 +0000782 MI->addImmOperand(Flags);
Chris Lattnerc3a9f8d2006-02-23 19:21:04 +0000783 ++i; // Skip the ID value.
784
785 switch (Flags & 7) {
Chris Lattneracc43bf2006-01-26 23:28:04 +0000786 default: assert(0 && "Bad flags!");
Chris Lattnerc3a9f8d2006-02-23 19:21:04 +0000787 case 1: // Use of register.
Chris Lattnerfd6d2822006-02-24 19:18:20 +0000788 for (; NumVals; --NumVals, ++i) {
Chris Lattnerc3a9f8d2006-02-23 19:21:04 +0000789 unsigned Reg = cast<RegisterSDNode>(Node->getOperand(i))->getReg();
Chris Lattner09e46062006-09-05 02:31:13 +0000790 MI->addRegOperand(Reg, false);
Chris Lattnerc3a9f8d2006-02-23 19:21:04 +0000791 }
Chris Lattnerdc19b702006-02-04 02:26:14 +0000792 break;
Chris Lattnerc3a9f8d2006-02-23 19:21:04 +0000793 case 2: // Def of register.
Chris Lattnerfd6d2822006-02-24 19:18:20 +0000794 for (; NumVals; --NumVals, ++i) {
Chris Lattnerc3a9f8d2006-02-23 19:21:04 +0000795 unsigned Reg = cast<RegisterSDNode>(Node->getOperand(i))->getReg();
Chris Lattner09e46062006-09-05 02:31:13 +0000796 MI->addRegOperand(Reg, true);
Chris Lattnerc3a9f8d2006-02-23 19:21:04 +0000797 }
Chris Lattnerdc19b702006-02-04 02:26:14 +0000798 break;
Chris Lattnerdc19b702006-02-04 02:26:14 +0000799 case 3: { // Immediate.
Chris Lattner7df31dc2007-08-25 00:53:07 +0000800 for (; NumVals; --NumVals, ++i) {
801 if (ConstantSDNode *CS =
802 dyn_cast<ConstantSDNode>(Node->getOperand(i))) {
803 MI->addImmOperand(CS->getValue());
804 } else {
805 GlobalAddressSDNode *GA =
806 cast<GlobalAddressSDNode>(Node->getOperand(i));
807 MI->addGlobalAddressOperand(GA->getGlobal(), GA->getOffset());
808 }
Chris Lattnerefa46ce2006-10-31 20:01:56 +0000809 }
Chris Lattnerdc19b702006-02-04 02:26:14 +0000810 break;
811 }
Chris Lattnerfd6d2822006-02-24 19:18:20 +0000812 case 4: // Addressing mode.
813 // The addressing mode has been selected, just add all of the
814 // operands to the machine instruction.
815 for (; NumVals; --NumVals, ++i)
Chris Lattnerdf375062006-03-10 07:25:12 +0000816 AddOperand(MI, Node->getOperand(i), 0, 0, VRBaseMap);
Chris Lattnerfd6d2822006-02-24 19:18:20 +0000817 break;
Chris Lattnerdc19b702006-02-04 02:26:14 +0000818 }
Chris Lattneracc43bf2006-01-26 23:28:04 +0000819 }
820 break;
821 }
Jim Laskeyb6d4c2c2005-09-30 19:15:27 +0000822 }
823 }
Jim Laskeyb6d4c2c2005-09-30 19:15:27 +0000824}
825
Chris Lattnera93dfcd2006-03-05 23:51:47 +0000826void ScheduleDAG::EmitNoop() {
827 TII->insertNoop(*BB, BB->end());
828}
829
Evan Cheng42d60272007-09-26 21:36:17 +0000830void ScheduleDAG::EmitCrossRCCopy(SUnit *SU, DenseMap<SUnit*, unsigned> &VRBaseMap) {
831 for (SUnit::const_pred_iterator I = SU->Preds.begin(), E = SU->Preds.end();
832 I != E; ++I) {
833 if (I->isCtrl) continue; // ignore chain preds
834 if (!I->Dep->Node) {
835 // Copy to physical register.
836 DenseMap<SUnit*, unsigned>::iterator VRI = VRBaseMap.find(I->Dep);
837 assert(VRI != VRBaseMap.end() && "Node emitted out of order - late");
838 // Find the destination physical register.
839 unsigned Reg = 0;
840 for (SUnit::const_succ_iterator II = SU->Succs.begin(),
841 EE = SU->Succs.end(); II != EE; ++II) {
842 if (I->Reg) {
843 Reg = I->Reg;
844 break;
845 }
846 }
847 assert(I->Reg && "Unknown physical register!");
848 MRI->copyRegToReg(*BB, BB->end(), Reg, VRI->second,
849 SU->CopyDstRC, SU->CopySrcRC);
850 } else {
851 // Copy from physical register.
852 assert(I->Reg && "Unknown physical register!");
853 unsigned VRBase = RegMap->createVirtualRegister(SU->CopyDstRC);
854 bool isNew = VRBaseMap.insert(std::make_pair(SU, VRBase));
855 assert(isNew && "Node emitted out of order - early");
856 MRI->copyRegToReg(*BB, BB->end(), VRBase, I->Reg,
857 SU->CopyDstRC, SU->CopySrcRC);
858 }
859 break;
860 }
861}
862
Evan Chenge165a782006-05-11 23:55:42 +0000863/// EmitSchedule - Emit the machine code in scheduled order.
864void ScheduleDAG::EmitSchedule() {
Chris Lattner96645412006-05-16 06:10:58 +0000865 // If this is the first basic block in the function, and if it has live ins
866 // that need to be copied into vregs, emit the copies into the top of the
867 // block before emitting the code for the block.
868 MachineFunction &MF = DAG.getMachineFunction();
869 if (&MF.front() == BB && MF.livein_begin() != MF.livein_end()) {
870 for (MachineFunction::livein_iterator LI = MF.livein_begin(),
871 E = MF.livein_end(); LI != E; ++LI)
Evan Cheng9efce632007-09-26 06:25:56 +0000872 if (LI->second) {
873 const TargetRegisterClass *RC = RegMap->getRegClass(LI->second);
Chris Lattner96645412006-05-16 06:10:58 +0000874 MRI->copyRegToReg(*MF.begin(), MF.begin()->end(), LI->second,
Evan Cheng9efce632007-09-26 06:25:56 +0000875 LI->first, RC, RC);
876 }
Chris Lattner96645412006-05-16 06:10:58 +0000877 }
878
879
880 // Finally, emit the code for all of the scheduled instructions.
Evan Chengaf825c82007-07-10 07:08:32 +0000881 DenseMap<SDOperand, unsigned> VRBaseMap;
Evan Cheng42d60272007-09-26 21:36:17 +0000882 DenseMap<SUnit*, unsigned> CopyVRBaseMap;
Evan Chenge165a782006-05-11 23:55:42 +0000883 for (unsigned i = 0, e = Sequence.size(); i != e; i++) {
884 if (SUnit *SU = Sequence[i]) {
Evan Chenga6fb1b62007-09-25 01:54:36 +0000885 for (unsigned j = 0, ee = SU->FlaggedNodes.size(); j != ee; ++j)
886 EmitNode(SU->FlaggedNodes[j], SU->InstanceNo, VRBaseMap);
Evan Cheng42d60272007-09-26 21:36:17 +0000887 if (SU->Node)
888 EmitNode(SU->Node, SU->InstanceNo, VRBaseMap);
889 else
890 EmitCrossRCCopy(SU, CopyVRBaseMap);
Evan Chenge165a782006-05-11 23:55:42 +0000891 } else {
892 // Null SUnit* is a noop.
893 EmitNoop();
894 }
895 }
896}
897
898/// dump - dump the schedule.
899void ScheduleDAG::dumpSchedule() const {
900 for (unsigned i = 0, e = Sequence.size(); i != e; i++) {
901 if (SUnit *SU = Sequence[i])
902 SU->dump(&DAG);
903 else
Bill Wendling832171c2006-12-07 20:04:42 +0000904 cerr << "**** NOOP ****\n";
Evan Chenge165a782006-05-11 23:55:42 +0000905 }
906}
907
908
Evan Chenga9c20912006-01-21 02:32:06 +0000909/// Run - perform scheduling.
910///
911MachineBasicBlock *ScheduleDAG::Run() {
912 TII = TM.getInstrInfo();
913 MRI = TM.getRegisterInfo();
914 RegMap = BB->getParent()->getSSARegMap();
915 ConstPool = BB->getParent()->getConstantPool();
Evan Cheng4ef10862006-01-23 07:01:07 +0000916
Evan Chenga9c20912006-01-21 02:32:06 +0000917 Schedule();
918 return BB;
Chris Lattnerd32b2362005-08-18 18:45:24 +0000919}
Evan Cheng4ef10862006-01-23 07:01:07 +0000920
Evan Chenge165a782006-05-11 23:55:42 +0000921/// 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 {
Bill Wendling832171c2006-12-07 20:04:42 +0000924 cerr << "SU(" << NodeNum << "): ";
Evan Cheng42d60272007-09-26 21:36:17 +0000925 if (Node)
926 Node->dump(G);
927 else
928 cerr << "CROSS RC COPY ";
Bill Wendling832171c2006-12-07 20:04:42 +0000929 cerr << "\n";
Evan Chenge165a782006-05-11 23:55:42 +0000930 if (FlaggedNodes.size() != 0) {
931 for (unsigned i = 0, e = FlaggedNodes.size(); i != e; i++) {
Bill Wendling832171c2006-12-07 20:04:42 +0000932 cerr << " ";
Evan Chenge165a782006-05-11 23:55:42 +0000933 FlaggedNodes[i]->dump(G);
Bill Wendling832171c2006-12-07 20:04:42 +0000934 cerr << "\n";
Evan Chenge165a782006-05-11 23:55:42 +0000935 }
936 }
937}
Evan Cheng4ef10862006-01-23 07:01:07 +0000938
Evan Chenge165a782006-05-11 23:55:42 +0000939void SUnit::dumpAll(const SelectionDAG *G) const {
940 dump(G);
941
Bill Wendling832171c2006-12-07 20:04:42 +0000942 cerr << " # preds left : " << NumPredsLeft << "\n";
943 cerr << " # succs left : " << NumSuccsLeft << "\n";
944 cerr << " # chain preds left : " << NumChainPredsLeft << "\n";
945 cerr << " # chain succs left : " << NumChainSuccsLeft << "\n";
946 cerr << " Latency : " << Latency << "\n";
947 cerr << " Depth : " << Depth << "\n";
948 cerr << " Height : " << Height << "\n";
Evan Chenge165a782006-05-11 23:55:42 +0000949
950 if (Preds.size() != 0) {
Bill Wendling832171c2006-12-07 20:04:42 +0000951 cerr << " Predecessors:\n";
Chris Lattner228a18e2006-08-17 00:09:56 +0000952 for (SUnit::const_succ_iterator I = Preds.begin(), E = Preds.end();
953 I != E; ++I) {
Evan Cheng713a98d2007-09-19 01:38:40 +0000954 if (I->isCtrl)
Bill Wendling832171c2006-12-07 20:04:42 +0000955 cerr << " ch #";
Evan Chenge165a782006-05-11 23:55:42 +0000956 else
Bill Wendling832171c2006-12-07 20:04:42 +0000957 cerr << " val #";
Evan Chenga6fb1b62007-09-25 01:54:36 +0000958 cerr << I->Dep << " - SU(" << I->Dep->NodeNum << ")";
959 if (I->isSpecial)
960 cerr << " *";
961 cerr << "\n";
Evan Chenge165a782006-05-11 23:55:42 +0000962 }
963 }
964 if (Succs.size() != 0) {
Bill Wendling832171c2006-12-07 20:04:42 +0000965 cerr << " Successors:\n";
Chris Lattner228a18e2006-08-17 00:09:56 +0000966 for (SUnit::const_succ_iterator I = Succs.begin(), E = Succs.end();
967 I != E; ++I) {
Evan Cheng713a98d2007-09-19 01:38:40 +0000968 if (I->isCtrl)
Bill Wendling832171c2006-12-07 20:04:42 +0000969 cerr << " ch #";
Evan Chenge165a782006-05-11 23:55:42 +0000970 else
Bill Wendling832171c2006-12-07 20:04:42 +0000971 cerr << " val #";
Evan Chenga6fb1b62007-09-25 01:54:36 +0000972 cerr << I->Dep << " - SU(" << I->Dep->NodeNum << ")";
973 if (I->isSpecial)
974 cerr << " *";
975 cerr << "\n";
Evan Chenge165a782006-05-11 23:55:42 +0000976 }
977 }
Bill Wendling832171c2006-12-07 20:04:42 +0000978 cerr << "\n";
Evan Chenge165a782006-05-11 23:55:42 +0000979}