blob: a6e32b2bfad142269b8c9ef85682aa7a3df1a9e2 [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
31/// getPhysicalRegisterRegClass - Returns the Register Class of a physical
32/// register.
33static const TargetRegisterClass *getPhysicalRegisterRegClass(
34 const MRegisterInfo *MRI,
35 MVT::ValueType VT,
36 unsigned reg) {
37 assert(MRegisterInfo::isPhysicalRegister(reg) &&
38 "reg must be a physical register");
39 // Pick the register class of the right type that contains this physreg.
40 for (MRegisterInfo::regclass_iterator I = MRI->regclass_begin(),
41 E = MRI->regclass_end(); I != E; ++I)
42 if ((*I)->hasType(VT) && (*I)->contains(reg))
43 return *I;
44 assert(false && "Couldn't find the register class");
45 return 0;
46}
47
48
49/// CheckForPhysRegDependency - Check if the dependency between def and use of
50/// a specified operand is a physical register dependency. If so, returns the
51/// register and the cost of copying the register.
52static void CheckForPhysRegDependency(SDNode *Def, SDNode *Use, unsigned Op,
53 const MRegisterInfo *MRI,
54 const TargetInstrInfo *TII,
55 unsigned &PhysReg, int &Cost) {
56 if (Op != 2 || Use->getOpcode() != ISD::CopyToReg)
57 return;
58
59 unsigned Reg = cast<RegisterSDNode>(Use->getOperand(1))->getReg();
60 if (MRegisterInfo::isVirtualRegister(Reg))
61 return;
62
63 unsigned ResNo = Use->getOperand(2).ResNo;
64 if (Def->isTargetOpcode()) {
65 const TargetInstrDescriptor &II = TII->get(Def->getTargetOpcode());
66 if (ResNo >= II.numDefs &&
67 II.ImplicitDefs[ResNo - II.numDefs] == Reg) {
68 PhysReg = Reg;
69 const TargetRegisterClass *RC =
70 getPhysicalRegisterRegClass(MRI, Def->getValueType(ResNo), Reg);
71 Cost = RC->getCopyCost();
72 }
73 }
74}
75
76SUnit *ScheduleDAG::Clone(SUnit *Old) {
77 SUnit *SU = NewSUnit(Old->Node);
78 for (unsigned i = 0, e = SU->FlaggedNodes.size(); i != e; ++i)
79 SU->FlaggedNodes.push_back(SU->FlaggedNodes[i]);
80 SU->InstanceNo = SUnitMap[Old->Node].size();
81 SU->Latency = Old->Latency;
82 SU->isTwoAddress = Old->isTwoAddress;
83 SU->isCommutable = Old->isCommutable;
84 SU->hasImplicitDefs = Old->hasImplicitDefs;
85 SUnitMap[Old->Node].push_back(SU);
86 return SU;
87}
88
Evan Chenge165a782006-05-11 23:55:42 +000089/// BuildSchedUnits - Build SUnits from the selection dag that we are input.
90/// This SUnit graph is similar to the SelectionDAG, but represents flagged
91/// together nodes with a single SUnit.
92void ScheduleDAG::BuildSchedUnits() {
93 // Reserve entries in the vector for each of the SUnits we are creating. This
94 // ensure that reallocation of the vector won't happen, so SUnit*'s won't get
95 // invalidated.
96 SUnits.reserve(std::distance(DAG.allnodes_begin(), DAG.allnodes_end()));
97
98 const InstrItineraryData &InstrItins = TM.getInstrItineraryData();
99
100 for (SelectionDAG::allnodes_iterator NI = DAG.allnodes_begin(),
101 E = DAG.allnodes_end(); NI != E; ++NI) {
102 if (isPassiveNode(NI)) // Leaf node, e.g. a TargetImmediate.
103 continue;
104
105 // If this node has already been processed, stop now.
Evan Chenga6fb1b62007-09-25 01:54:36 +0000106 if (SUnitMap[NI].size()) continue;
Evan Chenge165a782006-05-11 23:55:42 +0000107
108 SUnit *NodeSUnit = NewSUnit(NI);
109
110 // See if anything is flagged to this node, if so, add them to flagged
111 // nodes. Nodes can have at most one flag input and one flag output. Flags
112 // are required the be the last operand and result of a node.
113
114 // Scan up, adding flagged preds to FlaggedNodes.
115 SDNode *N = NI;
Evan Cheng3b97acd2006-08-07 22:12:12 +0000116 if (N->getNumOperands() &&
117 N->getOperand(N->getNumOperands()-1).getValueType() == MVT::Flag) {
118 do {
119 N = N->getOperand(N->getNumOperands()-1).Val;
120 NodeSUnit->FlaggedNodes.push_back(N);
Evan Chenga6fb1b62007-09-25 01:54:36 +0000121 SUnitMap[N].push_back(NodeSUnit);
Evan Cheng3b97acd2006-08-07 22:12:12 +0000122 } while (N->getNumOperands() &&
123 N->getOperand(N->getNumOperands()-1).getValueType()== MVT::Flag);
124 std::reverse(NodeSUnit->FlaggedNodes.begin(),
125 NodeSUnit->FlaggedNodes.end());
Evan Chenge165a782006-05-11 23:55:42 +0000126 }
127
128 // Scan down, adding this node and any flagged succs to FlaggedNodes if they
129 // have a user of the flag operand.
130 N = NI;
131 while (N->getValueType(N->getNumValues()-1) == MVT::Flag) {
132 SDOperand FlagVal(N, N->getNumValues()-1);
133
134 // There are either zero or one users of the Flag result.
135 bool HasFlagUse = false;
136 for (SDNode::use_iterator UI = N->use_begin(), E = N->use_end();
137 UI != E; ++UI)
138 if (FlagVal.isOperand(*UI)) {
139 HasFlagUse = true;
140 NodeSUnit->FlaggedNodes.push_back(N);
Evan Chenga6fb1b62007-09-25 01:54:36 +0000141 SUnitMap[N].push_back(NodeSUnit);
Evan Chenge165a782006-05-11 23:55:42 +0000142 N = *UI;
143 break;
144 }
Chris Lattner228a18e2006-08-17 00:09:56 +0000145 if (!HasFlagUse) break;
Evan Chenge165a782006-05-11 23:55:42 +0000146 }
147
148 // Now all flagged nodes are in FlaggedNodes and N is the bottom-most node.
149 // Update the SUnit
150 NodeSUnit->Node = N;
Evan Chenga6fb1b62007-09-25 01:54:36 +0000151 SUnitMap[N].push_back(NodeSUnit);
Evan Chenge165a782006-05-11 23:55:42 +0000152
153 // Compute the latency for the node. We use the sum of the latencies for
154 // all nodes flagged together into this SUnit.
155 if (InstrItins.isEmpty()) {
156 // No latency information.
157 NodeSUnit->Latency = 1;
158 } else {
159 NodeSUnit->Latency = 0;
160 if (N->isTargetOpcode()) {
161 unsigned SchedClass = TII->getSchedClass(N->getTargetOpcode());
162 InstrStage *S = InstrItins.begin(SchedClass);
163 InstrStage *E = InstrItins.end(SchedClass);
164 for (; S != E; ++S)
165 NodeSUnit->Latency += S->Cycles;
166 }
167 for (unsigned i = 0, e = NodeSUnit->FlaggedNodes.size(); i != e; ++i) {
168 SDNode *FNode = NodeSUnit->FlaggedNodes[i];
169 if (FNode->isTargetOpcode()) {
170 unsigned SchedClass = TII->getSchedClass(FNode->getTargetOpcode());
171 InstrStage *S = InstrItins.begin(SchedClass);
172 InstrStage *E = InstrItins.end(SchedClass);
173 for (; S != E; ++S)
174 NodeSUnit->Latency += S->Cycles;
175 }
176 }
177 }
178 }
179
180 // Pass 2: add the preds, succs, etc.
181 for (unsigned su = 0, e = SUnits.size(); su != e; ++su) {
182 SUnit *SU = &SUnits[su];
183 SDNode *MainNode = SU->Node;
184
185 if (MainNode->isTargetOpcode()) {
186 unsigned Opc = MainNode->getTargetOpcode();
Evan Chenga6fb1b62007-09-25 01:54:36 +0000187 const TargetInstrDescriptor &TID = TII->get(Opc);
188 if (TID.ImplicitDefs)
189 SU->hasImplicitDefs = true;
190 for (unsigned i = 0; i != TID.numOperands; ++i) {
191 if (TID.getOperandConstraint(i, TOI::TIED_TO) != -1) {
Evan Cheng95f6ede2006-11-04 09:44:31 +0000192 SU->isTwoAddress = true;
193 break;
194 }
195 }
Evan Chenga6fb1b62007-09-25 01:54:36 +0000196 if (TID.Flags & M_COMMUTABLE)
Evan Cheng13d41b92006-05-12 01:58:24 +0000197 SU->isCommutable = true;
Evan Chenge165a782006-05-11 23:55:42 +0000198 }
199
200 // Find all predecessors and successors of the group.
201 // Temporarily add N to make code simpler.
202 SU->FlaggedNodes.push_back(MainNode);
203
204 for (unsigned n = 0, e = SU->FlaggedNodes.size(); n != e; ++n) {
205 SDNode *N = SU->FlaggedNodes[n];
Evan Chenga6fb1b62007-09-25 01:54:36 +0000206 if (N->isTargetOpcode() && TII->getImplicitDefs(N->getTargetOpcode()))
207 SU->hasImplicitDefs = true;
Evan Chenge165a782006-05-11 23:55:42 +0000208
209 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
210 SDNode *OpN = N->getOperand(i).Val;
211 if (isPassiveNode(OpN)) continue; // Not scheduled.
Evan Chenga6fb1b62007-09-25 01:54:36 +0000212 SUnit *OpSU = SUnitMap[OpN].front();
Evan Chenge165a782006-05-11 23:55:42 +0000213 assert(OpSU && "Node has no SUnit!");
214 if (OpSU == SU) continue; // In the same group.
215
216 MVT::ValueType OpVT = N->getOperand(i).getValueType();
217 assert(OpVT != MVT::Flag && "Flagged nodes should be in same sunit!");
218 bool isChain = OpVT == MVT::Other;
Evan Chenga6fb1b62007-09-25 01:54:36 +0000219
220 unsigned PhysReg = 0;
221 int Cost = 1;
222 // Determine if this is a physical register dependency.
223 CheckForPhysRegDependency(OpN, N, i, MRI, TII, PhysReg, Cost);
224 SU->addPred(OpSU, isChain, false, PhysReg, Cost);
Evan Chenge165a782006-05-11 23:55:42 +0000225 }
226 }
227
228 // Remove MainNode from FlaggedNodes again.
229 SU->FlaggedNodes.pop_back();
230 }
231
232 return;
233}
234
Evan Chenge165a782006-05-11 23:55:42 +0000235void ScheduleDAG::CalculateDepths() {
Evan Cheng99126282007-07-06 01:37:28 +0000236 std::vector<std::pair<SUnit*, unsigned> > WorkList;
Evan Chenge165a782006-05-11 23:55:42 +0000237 for (unsigned i = 0, e = SUnits.size(); i != e; ++i)
Evan Cheng69001322007-09-12 23:45:46 +0000238 if (SUnits[i].Preds.size() == 0)
Evan Cheng99126282007-07-06 01:37:28 +0000239 WorkList.push_back(std::make_pair(&SUnits[i], 0U));
Evan Chenge165a782006-05-11 23:55:42 +0000240
Evan Cheng99126282007-07-06 01:37:28 +0000241 while (!WorkList.empty()) {
242 SUnit *SU = WorkList.back().first;
243 unsigned Depth = WorkList.back().second;
244 WorkList.pop_back();
245 if (SU->Depth == 0 || Depth > SU->Depth) {
246 SU->Depth = Depth;
247 for (SUnit::succ_iterator I = SU->Succs.begin(), E = SU->Succs.end();
248 I != E; ++I)
Evan Cheng713a98d2007-09-19 01:38:40 +0000249 WorkList.push_back(std::make_pair(I->Dep, Depth+1));
Evan Cheng99126282007-07-06 01:37:28 +0000250 }
Evan Cheng626da3d2006-05-12 06:05:18 +0000251 }
Evan Chenge165a782006-05-11 23:55:42 +0000252}
Evan Cheng99126282007-07-06 01:37:28 +0000253
Evan Chenge165a782006-05-11 23:55:42 +0000254void ScheduleDAG::CalculateHeights() {
Evan Cheng99126282007-07-06 01:37:28 +0000255 std::vector<std::pair<SUnit*, unsigned> > WorkList;
Evan Chenga6fb1b62007-09-25 01:54:36 +0000256 SUnit *Root = SUnitMap[DAG.getRoot().Val].front();
Evan Cheng99126282007-07-06 01:37:28 +0000257 WorkList.push_back(std::make_pair(Root, 0U));
258
259 while (!WorkList.empty()) {
260 SUnit *SU = WorkList.back().first;
261 unsigned Height = WorkList.back().second;
262 WorkList.pop_back();
263 if (SU->Height == 0 || Height > SU->Height) {
264 SU->Height = Height;
265 for (SUnit::pred_iterator I = SU->Preds.begin(), E = SU->Preds.end();
266 I != E; ++I)
Evan Cheng713a98d2007-09-19 01:38:40 +0000267 WorkList.push_back(std::make_pair(I->Dep, Height+1));
Evan Cheng99126282007-07-06 01:37:28 +0000268 }
269 }
Evan Chenge165a782006-05-11 23:55:42 +0000270}
271
Jim Laskeye6b90fb2005-09-26 21:57:04 +0000272/// CountResults - The results of target nodes have register or immediate
273/// operands first, then an optional chain, and optional flag operands (which do
274/// not go into the machine instrs.)
Evan Cheng95f6ede2006-11-04 09:44:31 +0000275unsigned ScheduleDAG::CountResults(SDNode *Node) {
Jim Laskeyb6d4c2c2005-09-30 19:15:27 +0000276 unsigned N = Node->getNumValues();
277 while (N && Node->getValueType(N - 1) == MVT::Flag)
Jim Laskeye6b90fb2005-09-26 21:57:04 +0000278 --N;
Jim Laskeyb6d4c2c2005-09-30 19:15:27 +0000279 if (N && Node->getValueType(N - 1) == MVT::Other)
Jim Laskeye6b90fb2005-09-26 21:57:04 +0000280 --N; // Skip over chain result.
281 return N;
282}
283
284/// CountOperands The inputs to target nodes have any actual inputs first,
285/// followed by an optional chain operand, then flag operands. Compute the
286/// number of actual operands that will go into the machine instr.
Evan Cheng95f6ede2006-11-04 09:44:31 +0000287unsigned ScheduleDAG::CountOperands(SDNode *Node) {
Jim Laskeyb6d4c2c2005-09-30 19:15:27 +0000288 unsigned N = Node->getNumOperands();
289 while (N && Node->getOperand(N - 1).getValueType() == MVT::Flag)
Jim Laskeye6b90fb2005-09-26 21:57:04 +0000290 --N;
Jim Laskeyb6d4c2c2005-09-30 19:15:27 +0000291 if (N && Node->getOperand(N - 1).getValueType() == MVT::Other)
Jim Laskeye6b90fb2005-09-26 21:57:04 +0000292 --N; // Ignore chain if it exists.
293 return N;
294}
295
Jim Laskey60f09922006-07-21 20:57:35 +0000296static const TargetRegisterClass *getInstrOperandRegClass(
297 const MRegisterInfo *MRI,
298 const TargetInstrInfo *TII,
299 const TargetInstrDescriptor *II,
300 unsigned Op) {
301 if (Op >= II->numOperands) {
302 assert((II->Flags & M_VARIABLE_OPS)&& "Invalid operand # of instruction");
303 return NULL;
304 }
305 const TargetOperandInfo &toi = II->OpInfo[Op];
306 return (toi.Flags & M_LOOK_UP_PTR_REG_CLASS)
307 ? TII->getPointerRegClass() : MRI->getRegClass(toi.RegClass);
308}
309
Evan Chenga6fb1b62007-09-25 01:54:36 +0000310void ScheduleDAG::EmitCopyFromReg(SDNode *Node, unsigned ResNo,
311 unsigned InstanceNo, unsigned SrcReg,
Evan Cheng84097472007-08-02 00:28:15 +0000312 DenseMap<SDOperand, unsigned> &VRBaseMap) {
313 unsigned VRBase = 0;
314 if (MRegisterInfo::isVirtualRegister(SrcReg)) {
315 // Just use the input register directly!
Evan Chenga6fb1b62007-09-25 01:54:36 +0000316 if (InstanceNo > 0)
317 VRBaseMap.erase(SDOperand(Node, ResNo));
Evan Cheng84097472007-08-02 00:28:15 +0000318 bool isNew = VRBaseMap.insert(std::make_pair(SDOperand(Node,ResNo),SrcReg));
319 assert(isNew && "Node emitted out of order - early");
320 return;
321 }
322
323 // If the node is only used by a CopyToReg and the dest reg is a vreg, use
324 // the CopyToReg'd destination register instead of creating a new vreg.
Evan Chenga6fb1b62007-09-25 01:54:36 +0000325 bool MatchReg = true;
Evan Cheng84097472007-08-02 00:28:15 +0000326 for (SDNode::use_iterator UI = Node->use_begin(), E = Node->use_end();
327 UI != E; ++UI) {
328 SDNode *Use = *UI;
Evan Chenga6fb1b62007-09-25 01:54:36 +0000329 bool Match = true;
Evan Cheng84097472007-08-02 00:28:15 +0000330 if (Use->getOpcode() == ISD::CopyToReg &&
331 Use->getOperand(2).Val == Node &&
332 Use->getOperand(2).ResNo == ResNo) {
333 unsigned DestReg = cast<RegisterSDNode>(Use->getOperand(1))->getReg();
334 if (MRegisterInfo::isVirtualRegister(DestReg)) {
335 VRBase = DestReg;
Evan Chenga6fb1b62007-09-25 01:54:36 +0000336 Match = false;
337 } else if (DestReg != SrcReg)
338 Match = false;
339 } else {
340 for (unsigned i = 0, e = Use->getNumOperands(); i != e; ++i) {
341 SDOperand Op = Use->getOperand(i);
342 if (Op.Val != Node)
343 continue;
344 MVT::ValueType VT = Node->getValueType(Op.ResNo);
345 if (VT != MVT::Other && VT != MVT::Flag)
346 Match = false;
Evan Cheng84097472007-08-02 00:28:15 +0000347 }
348 }
Evan Chenga6fb1b62007-09-25 01:54:36 +0000349 MatchReg &= Match;
350 if (VRBase)
351 break;
Evan Cheng84097472007-08-02 00:28:15 +0000352 }
353
Evan Cheng84097472007-08-02 00:28:15 +0000354 const TargetRegisterClass *TRC = 0;
Evan Chenga6fb1b62007-09-25 01:54:36 +0000355 // Figure out the register class to create for the destreg.
356 if (VRBase)
Evan Cheng84097472007-08-02 00:28:15 +0000357 TRC = RegMap->getRegClass(VRBase);
Evan Chenga6fb1b62007-09-25 01:54:36 +0000358 else
Evan Cheng84097472007-08-02 00:28:15 +0000359 TRC = getPhysicalRegisterRegClass(MRI, Node->getValueType(ResNo), SrcReg);
Evan Chenga6fb1b62007-09-25 01:54:36 +0000360
361 // If all uses are reading from the src physical register and copying the
362 // register is either impossible or very expensive, then don't create a copy.
363 if (MatchReg && TRC->getCopyCost() < 0) {
364 VRBase = SrcReg;
365 } else {
Evan Cheng84097472007-08-02 00:28:15 +0000366 // Create the reg, emit the copy.
367 VRBase = RegMap->createVirtualRegister(TRC);
Evan Cheng9efce632007-09-26 06:25:56 +0000368 MRI->copyRegToReg(*BB, BB->end(), VRBase, SrcReg, TRC, TRC);
Evan Cheng84097472007-08-02 00:28:15 +0000369 }
Evan Cheng84097472007-08-02 00:28:15 +0000370
Evan Chenga6fb1b62007-09-25 01:54:36 +0000371 if (InstanceNo > 0)
372 VRBaseMap.erase(SDOperand(Node, ResNo));
Evan Cheng84097472007-08-02 00:28:15 +0000373 bool isNew = VRBaseMap.insert(std::make_pair(SDOperand(Node,ResNo), VRBase));
374 assert(isNew && "Node emitted out of order - early");
375}
376
377void ScheduleDAG::CreateVirtualRegisters(SDNode *Node,
378 MachineInstr *MI,
379 const TargetInstrDescriptor &II,
380 DenseMap<SDOperand, unsigned> &VRBaseMap) {
381 for (unsigned i = 0; i < II.numDefs; ++i) {
Evan Chengaf825c82007-07-10 07:08:32 +0000382 // If the specific node value is only used by a CopyToReg and the dest reg
383 // is a vreg, use the CopyToReg'd destination register instead of creating
384 // a new vreg.
385 unsigned VRBase = 0;
386 for (SDNode::use_iterator UI = Node->use_begin(), E = Node->use_end();
387 UI != E; ++UI) {
388 SDNode *Use = *UI;
389 if (Use->getOpcode() == ISD::CopyToReg &&
390 Use->getOperand(2).Val == Node &&
391 Use->getOperand(2).ResNo == i) {
392 unsigned Reg = cast<RegisterSDNode>(Use->getOperand(1))->getReg();
393 if (MRegisterInfo::isVirtualRegister(Reg)) {
394 VRBase = Reg;
395 MI->addRegOperand(Reg, true);
396 break;
397 }
398 }
399 }
400
Evan Cheng84097472007-08-02 00:28:15 +0000401 // Create the result registers for this node and add the result regs to
402 // the machine instruction.
Evan Chengaf825c82007-07-10 07:08:32 +0000403 if (VRBase == 0) {
Evan Chengaf825c82007-07-10 07:08:32 +0000404 const TargetRegisterClass *RC = getInstrOperandRegClass(MRI, TII, &II, i);
405 assert(RC && "Isn't a register operand!");
406 VRBase = RegMap->createVirtualRegister(RC);
407 MI->addRegOperand(VRBase, true);
408 }
409
410 bool isNew = VRBaseMap.insert(std::make_pair(SDOperand(Node,i), VRBase));
411 assert(isNew && "Node emitted out of order - early");
Jim Laskeye6b90fb2005-09-26 21:57:04 +0000412 }
Jim Laskeye6b90fb2005-09-26 21:57:04 +0000413}
414
Chris Lattnerdf375062006-03-10 07:25:12 +0000415/// getVR - Return the virtual register corresponding to the specified result
416/// of the specified node.
Evan Chengaf825c82007-07-10 07:08:32 +0000417static unsigned getVR(SDOperand Op, DenseMap<SDOperand, unsigned> &VRBaseMap) {
418 DenseMap<SDOperand, unsigned>::iterator I = VRBaseMap.find(Op);
Chris Lattnerdf375062006-03-10 07:25:12 +0000419 assert(I != VRBaseMap.end() && "Node emitted out of order - late");
Evan Chengaf825c82007-07-10 07:08:32 +0000420 return I->second;
Chris Lattnerdf375062006-03-10 07:25:12 +0000421}
422
423
Chris Lattnered18b682006-02-24 18:54:03 +0000424/// AddOperand - Add the specified operand to the specified machine instr. II
425/// specifies the instruction information for the node, and IIOpNum is the
426/// operand number (in the II) that we are adding. IIOpNum and II are used for
427/// assertions only.
428void ScheduleDAG::AddOperand(MachineInstr *MI, SDOperand Op,
429 unsigned IIOpNum,
Chris Lattnerdf375062006-03-10 07:25:12 +0000430 const TargetInstrDescriptor *II,
Evan Chengaf825c82007-07-10 07:08:32 +0000431 DenseMap<SDOperand, unsigned> &VRBaseMap) {
Chris Lattnered18b682006-02-24 18:54:03 +0000432 if (Op.isTargetOpcode()) {
433 // Note that this case is redundant with the final else block, but we
434 // include it because it is the most common and it makes the logic
435 // simpler here.
436 assert(Op.getValueType() != MVT::Other &&
437 Op.getValueType() != MVT::Flag &&
438 "Chain and flag operands should occur at end of operand list!");
439
440 // Get/emit the operand.
Chris Lattnerdf375062006-03-10 07:25:12 +0000441 unsigned VReg = getVR(Op, VRBaseMap);
Evan Cheng5e2456c2007-07-10 17:52:20 +0000442 const TargetInstrDescriptor *TID = MI->getInstrDescriptor();
443 bool isOptDef = (IIOpNum < TID->numOperands)
444 ? (TID->OpInfo[IIOpNum].Flags & M_OPTIONAL_DEF_OPERAND) : false;
445 MI->addRegOperand(VReg, isOptDef);
Chris Lattnered18b682006-02-24 18:54:03 +0000446
447 // Verify that it is right.
448 assert(MRegisterInfo::isVirtualRegister(VReg) && "Not a vreg?");
449 if (II) {
Jim Laskey60f09922006-07-21 20:57:35 +0000450 const TargetRegisterClass *RC =
451 getInstrOperandRegClass(MRI, TII, II, IIOpNum);
Evan Cheng21d03f22006-05-18 20:42:07 +0000452 assert(RC && "Don't have operand info for this instruction!");
Chris Lattner01528292007-02-15 18:17:56 +0000453 const TargetRegisterClass *VRC = RegMap->getRegClass(VReg);
454 if (VRC != RC) {
455 cerr << "Register class of operand and regclass of use don't agree!\n";
456#ifndef NDEBUG
457 cerr << "Operand = " << IIOpNum << "\n";
Chris Lattner95ad9432007-02-17 06:38:37 +0000458 cerr << "Op->Val = "; Op.Val->dump(&DAG); cerr << "\n";
Chris Lattner01528292007-02-15 18:17:56 +0000459 cerr << "MI = "; MI->print(cerr);
460 cerr << "VReg = " << VReg << "\n";
461 cerr << "VReg RegClass size = " << VRC->getSize()
Chris Lattner5d4a9f72007-02-15 18:19:15 +0000462 << ", align = " << VRC->getAlignment() << "\n";
Chris Lattner01528292007-02-15 18:17:56 +0000463 cerr << "Expected RegClass size = " << RC->getSize()
Chris Lattner5d4a9f72007-02-15 18:19:15 +0000464 << ", align = " << RC->getAlignment() << "\n";
Chris Lattner01528292007-02-15 18:17:56 +0000465#endif
466 cerr << "Fatal error, aborting.\n";
467 abort();
468 }
Chris Lattnered18b682006-02-24 18:54:03 +0000469 }
470 } else if (ConstantSDNode *C =
471 dyn_cast<ConstantSDNode>(Op)) {
Chris Lattner2d90ac72006-05-04 18:05:43 +0000472 MI->addImmOperand(C->getValue());
Evan Cheng489a87c2007-01-05 20:59:06 +0000473 } else if (RegisterSDNode *R =
Chris Lattnered18b682006-02-24 18:54:03 +0000474 dyn_cast<RegisterSDNode>(Op)) {
Chris Lattner09e46062006-09-05 02:31:13 +0000475 MI->addRegOperand(R->getReg(), false);
Chris Lattnered18b682006-02-24 18:54:03 +0000476 } else if (GlobalAddressSDNode *TGA =
477 dyn_cast<GlobalAddressSDNode>(Op)) {
Chris Lattnerea50fab2006-05-04 01:15:02 +0000478 MI->addGlobalAddressOperand(TGA->getGlobal(), TGA->getOffset());
Chris Lattnered18b682006-02-24 18:54:03 +0000479 } else if (BasicBlockSDNode *BB =
480 dyn_cast<BasicBlockSDNode>(Op)) {
481 MI->addMachineBasicBlockOperand(BB->getBasicBlock());
482 } else if (FrameIndexSDNode *FI =
483 dyn_cast<FrameIndexSDNode>(Op)) {
484 MI->addFrameIndexOperand(FI->getIndex());
Nate Begeman37efe672006-04-22 18:53:45 +0000485 } else if (JumpTableSDNode *JT =
486 dyn_cast<JumpTableSDNode>(Op)) {
487 MI->addJumpTableIndexOperand(JT->getIndex());
Chris Lattnered18b682006-02-24 18:54:03 +0000488 } else if (ConstantPoolSDNode *CP =
489 dyn_cast<ConstantPoolSDNode>(Op)) {
Evan Cheng404cb4f2006-02-25 09:54:52 +0000490 int Offset = CP->getOffset();
Chris Lattnered18b682006-02-24 18:54:03 +0000491 unsigned Align = CP->getAlignment();
Evan Chengd6594ae2006-09-12 21:00:35 +0000492 const Type *Type = CP->getType();
Chris Lattnered18b682006-02-24 18:54:03 +0000493 // MachineConstantPool wants an explicit alignment.
494 if (Align == 0) {
Evan Chengde268f72007-01-24 07:03:39 +0000495 Align = TM.getTargetData()->getPreferredTypeAlignmentShift(Type);
Evan Chengf6d039a2007-01-22 23:13:55 +0000496 if (Align == 0) {
Reid Spencerac9dcb92007-02-15 03:39:18 +0000497 // Alignment of vector types. FIXME!
Evan Chengf6d039a2007-01-22 23:13:55 +0000498 Align = TM.getTargetData()->getTypeSize(Type);
499 Align = Log2_64(Align);
Chris Lattner54a30b92006-03-20 01:51:46 +0000500 }
Chris Lattnered18b682006-02-24 18:54:03 +0000501 }
502
Evan Chengd6594ae2006-09-12 21:00:35 +0000503 unsigned Idx;
504 if (CP->isMachineConstantPoolEntry())
505 Idx = ConstPool->getConstantPoolIndex(CP->getMachineCPVal(), Align);
506 else
507 Idx = ConstPool->getConstantPoolIndex(CP->getConstVal(), Align);
Evan Cheng404cb4f2006-02-25 09:54:52 +0000508 MI->addConstantPoolIndexOperand(Idx, Offset);
Chris Lattnered18b682006-02-24 18:54:03 +0000509 } else if (ExternalSymbolSDNode *ES =
510 dyn_cast<ExternalSymbolSDNode>(Op)) {
Chris Lattnerea50fab2006-05-04 01:15:02 +0000511 MI->addExternalSymbolOperand(ES->getSymbol());
Chris Lattnered18b682006-02-24 18:54:03 +0000512 } else {
513 assert(Op.getValueType() != MVT::Other &&
514 Op.getValueType() != MVT::Flag &&
515 "Chain and flag operands should occur at end of operand list!");
Chris Lattnerdf375062006-03-10 07:25:12 +0000516 unsigned VReg = getVR(Op, VRBaseMap);
Chris Lattner09e46062006-09-05 02:31:13 +0000517 MI->addRegOperand(VReg, false);
Chris Lattnered18b682006-02-24 18:54:03 +0000518
519 // Verify that it is right.
520 assert(MRegisterInfo::isVirtualRegister(VReg) && "Not a vreg?");
521 if (II) {
Jim Laskey60f09922006-07-21 20:57:35 +0000522 const TargetRegisterClass *RC =
523 getInstrOperandRegClass(MRI, TII, II, IIOpNum);
Evan Cheng21d03f22006-05-18 20:42:07 +0000524 assert(RC && "Don't have operand info for this instruction!");
525 assert(RegMap->getRegClass(VReg) == RC &&
Chris Lattnered18b682006-02-24 18:54:03 +0000526 "Register class of operand and regclass of use don't agree!");
527 }
528 }
529
530}
531
Christopher Lambe24f8f12007-07-26 08:12:07 +0000532// Returns the Register Class of a subregister
533static const TargetRegisterClass *getSubRegisterRegClass(
534 const TargetRegisterClass *TRC,
535 unsigned SubIdx) {
536 // Pick the register class of the subregister
537 MRegisterInfo::regclass_iterator I = TRC->subregclasses_begin() + SubIdx-1;
538 assert(I < TRC->subregclasses_end() &&
539 "Invalid subregister index for register class");
540 return *I;
541}
542
543static const TargetRegisterClass *getSuperregRegisterClass(
544 const TargetRegisterClass *TRC,
545 unsigned SubIdx,
546 MVT::ValueType VT) {
547 // Pick the register class of the superegister for this type
548 for (MRegisterInfo::regclass_iterator I = TRC->superregclasses_begin(),
549 E = TRC->superregclasses_end(); I != E; ++I)
550 if ((*I)->hasType(VT) && getSubRegisterRegClass(*I, SubIdx) == TRC)
551 return *I;
552 assert(false && "Couldn't find the register class");
553 return 0;
554}
555
556/// EmitSubregNode - Generate machine code for subreg nodes.
557///
558void ScheduleDAG::EmitSubregNode(SDNode *Node,
559 DenseMap<SDOperand, unsigned> &VRBaseMap) {
560 unsigned VRBase = 0;
561 unsigned Opc = Node->getTargetOpcode();
562 if (Opc == TargetInstrInfo::EXTRACT_SUBREG) {
563 // If the node is only used by a CopyToReg and the dest reg is a vreg, use
564 // the CopyToReg'd destination register instead of creating a new vreg.
565 for (SDNode::use_iterator UI = Node->use_begin(), E = Node->use_end();
566 UI != E; ++UI) {
567 SDNode *Use = *UI;
568 if (Use->getOpcode() == ISD::CopyToReg &&
569 Use->getOperand(2).Val == Node) {
570 unsigned DestReg = cast<RegisterSDNode>(Use->getOperand(1))->getReg();
571 if (MRegisterInfo::isVirtualRegister(DestReg)) {
572 VRBase = DestReg;
573 break;
574 }
575 }
576 }
577
578 unsigned SubIdx = cast<ConstantSDNode>(Node->getOperand(1))->getValue();
579
580 // TODO: If the node is a use of a CopyFromReg from a physical register
581 // fold the extract into the copy now
582
583 // TODO: Add tracking info to SSARegMap of which vregs are subregs
584 // to allow coalescing in the allocator
585
586 // Create the extract_subreg machine instruction.
587 MachineInstr *MI =
588 new MachineInstr(BB, TII->get(TargetInstrInfo::EXTRACT_SUBREG));
589
590 // Figure out the register class to create for the destreg.
591 unsigned VReg = getVR(Node->getOperand(0), VRBaseMap);
592 const TargetRegisterClass *TRC = RegMap->getRegClass(VReg);
593 const TargetRegisterClass *SRC = getSubRegisterRegClass(TRC, SubIdx);
594
595 if (VRBase) {
596 // Grab the destination register
597 const TargetRegisterClass *DRC = 0;
598 DRC = RegMap->getRegClass(VRBase);
599 assert(SRC == DRC &&
600 "Source subregister and destination must have the same class");
601 } else {
602 // Create the reg
603 VRBase = RegMap->createVirtualRegister(SRC);
604 }
605
606 // Add def, source, and subreg index
607 MI->addRegOperand(VRBase, true);
608 AddOperand(MI, Node->getOperand(0), 0, 0, VRBaseMap);
609 MI->addImmOperand(SubIdx);
610
611 } else if (Opc == TargetInstrInfo::INSERT_SUBREG) {
612 assert((Node->getNumOperands() == 2 || Node->getNumOperands() == 3) &&
613 "Malformed insert_subreg node");
614 bool isUndefInput = (Node->getNumOperands() == 2);
615 unsigned SubReg = 0;
616 unsigned SubIdx = 0;
617
618 if (isUndefInput) {
619 SubReg = getVR(Node->getOperand(0), VRBaseMap);
620 SubIdx = cast<ConstantSDNode>(Node->getOperand(1))->getValue();
621 } else {
622 SubReg = getVR(Node->getOperand(1), VRBaseMap);
623 SubIdx = cast<ConstantSDNode>(Node->getOperand(2))->getValue();
624 }
625
626 // TODO: Add tracking info to SSARegMap of which vregs are subregs
627 // to allow coalescing in the allocator
628
629 // If the node is only used by a CopyToReg and the dest reg is a vreg, use
630 // the CopyToReg'd destination register instead of creating a new vreg.
631 // If the CopyToReg'd destination register is physical, then fold the
632 // insert into the copy
633 for (SDNode::use_iterator UI = Node->use_begin(), E = Node->use_end();
634 UI != E; ++UI) {
635 SDNode *Use = *UI;
636 if (Use->getOpcode() == ISD::CopyToReg &&
637 Use->getOperand(2).Val == Node) {
638 unsigned DestReg = cast<RegisterSDNode>(Use->getOperand(1))->getReg();
639 if (MRegisterInfo::isVirtualRegister(DestReg)) {
640 VRBase = DestReg;
641 break;
642 }
643 }
644 }
645
646 // Create the insert_subreg machine instruction.
647 MachineInstr *MI =
648 new MachineInstr(BB, TII->get(TargetInstrInfo::INSERT_SUBREG));
649
650 // Figure out the register class to create for the destreg.
651 const TargetRegisterClass *TRC = 0;
652 if (VRBase) {
653 TRC = RegMap->getRegClass(VRBase);
654 } else {
655 TRC = getSuperregRegisterClass(RegMap->getRegClass(SubReg),
656 SubIdx,
657 Node->getValueType(0));
658 assert(TRC && "Couldn't determine register class for insert_subreg");
659 VRBase = RegMap->createVirtualRegister(TRC); // Create the reg
660 }
661
662 MI->addRegOperand(VRBase, true);
663 AddOperand(MI, Node->getOperand(0), 0, 0, VRBaseMap);
664 if (!isUndefInput)
665 AddOperand(MI, Node->getOperand(1), 0, 0, VRBaseMap);
666 MI->addImmOperand(SubIdx);
667 } else
668 assert(0 && "Node is not a subreg insert or extract");
669
670 bool isNew = VRBaseMap.insert(std::make_pair(SDOperand(Node,0), VRBase));
671 assert(isNew && "Node emitted out of order - early");
672}
673
Jim Laskeyb6d4c2c2005-09-30 19:15:27 +0000674/// EmitNode - Generate machine code for an node and needed dependencies.
Jim Laskeye6b90fb2005-09-26 21:57:04 +0000675///
Evan Chenga6fb1b62007-09-25 01:54:36 +0000676void ScheduleDAG::EmitNode(SDNode *Node, unsigned InstanceNo,
Evan Chengaf825c82007-07-10 07:08:32 +0000677 DenseMap<SDOperand, unsigned> &VRBaseMap) {
Jim Laskeyb6d4c2c2005-09-30 19:15:27 +0000678 // If machine instruction
679 if (Node->isTargetOpcode()) {
680 unsigned Opc = Node->getTargetOpcode();
Christopher Lambe24f8f12007-07-26 08:12:07 +0000681
682 // Handle subreg insert/extract specially
683 if (Opc == TargetInstrInfo::EXTRACT_SUBREG ||
684 Opc == TargetInstrInfo::INSERT_SUBREG) {
685 EmitSubregNode(Node, VRBaseMap);
686 return;
687 }
688
Evan Chenga9c20912006-01-21 02:32:06 +0000689 const TargetInstrDescriptor &II = TII->get(Opc);
Chris Lattner2d973e42005-08-18 20:07:59 +0000690
Jim Laskeyb6d4c2c2005-09-30 19:15:27 +0000691 unsigned NumResults = CountResults(Node);
692 unsigned NodeOperands = CountOperands(Node);
Jim Laskeye6b90fb2005-09-26 21:57:04 +0000693 unsigned NumMIOperands = NodeOperands + NumResults;
Evan Cheng84097472007-08-02 00:28:15 +0000694 bool HasPhysRegOuts = (NumResults > II.numDefs) && II.ImplicitDefs;
Chris Lattnerda8abb02005-09-01 18:44:10 +0000695#ifndef NDEBUG
Evan Cheng8d3af5e2006-06-15 07:22:16 +0000696 assert((unsigned(II.numOperands) == NumMIOperands ||
Evan Cheng84097472007-08-02 00:28:15 +0000697 HasPhysRegOuts || (II.Flags & M_VARIABLE_OPS)) &&
Chris Lattner2d973e42005-08-18 20:07:59 +0000698 "#operands for dag node doesn't match .td file!");
Chris Lattnerca6aa2f2005-08-19 01:01:34 +0000699#endif
Chris Lattner2d973e42005-08-18 20:07:59 +0000700
701 // Create the new machine instruction.
Evan Chengc0f64ff2006-11-27 23:37:22 +0000702 MachineInstr *MI = new MachineInstr(II);
Chris Lattner2d973e42005-08-18 20:07:59 +0000703
704 // Add result register values for things that are defined by this
705 // instruction.
Evan Chengaf825c82007-07-10 07:08:32 +0000706 if (NumResults)
Evan Cheng84097472007-08-02 00:28:15 +0000707 CreateVirtualRegisters(Node, MI, II, VRBaseMap);
Jim Laskeyb6d4c2c2005-09-30 19:15:27 +0000708
709 // Emit all of the actual operands of this instruction, adding them to the
710 // instruction as appropriate.
Chris Lattnered18b682006-02-24 18:54:03 +0000711 for (unsigned i = 0; i != NodeOperands; ++i)
Evan Cheng84097472007-08-02 00:28:15 +0000712 AddOperand(MI, Node->getOperand(i), i+II.numDefs, &II, VRBaseMap);
Evan Cheng13d41b92006-05-12 01:58:24 +0000713
714 // Commute node if it has been determined to be profitable.
715 if (CommuteSet.count(Node)) {
716 MachineInstr *NewMI = TII->commuteInstruction(MI);
717 if (NewMI == 0)
Bill Wendling832171c2006-12-07 20:04:42 +0000718 DOUT << "Sched: COMMUTING FAILED!\n";
Evan Cheng13d41b92006-05-12 01:58:24 +0000719 else {
Bill Wendling832171c2006-12-07 20:04:42 +0000720 DOUT << "Sched: COMMUTED TO: " << *NewMI;
Evan Cheng4c6f2f92006-05-31 18:03:39 +0000721 if (MI != NewMI) {
722 delete MI;
723 MI = NewMI;
724 }
Evan Cheng13d41b92006-05-12 01:58:24 +0000725 }
726 }
727
Jim Laskeyb6d4c2c2005-09-30 19:15:27 +0000728 // Now that we have emitted all operands, emit this instruction itself.
729 if ((II.Flags & M_USES_CUSTOM_DAG_SCHED_INSERTION) == 0) {
730 BB->insert(BB->end(), MI);
731 } else {
732 // Insert this instruction into the end of the basic block, potentially
733 // taking some custom action.
734 BB = DAG.getTargetLoweringInfo().InsertAtEndOfBasicBlock(MI, BB);
735 }
Evan Cheng84097472007-08-02 00:28:15 +0000736
737 // Additional results must be an physical register def.
738 if (HasPhysRegOuts) {
739 for (unsigned i = II.numDefs; i < NumResults; ++i) {
740 unsigned Reg = II.ImplicitDefs[i - II.numDefs];
Evan Cheng33d55952007-08-02 05:29:38 +0000741 if (Node->hasAnyUseOfValue(i))
Evan Chenga6fb1b62007-09-25 01:54:36 +0000742 EmitCopyFromReg(Node, i, InstanceNo, Reg, VRBaseMap);
Evan Cheng84097472007-08-02 00:28:15 +0000743 }
744 }
Jim Laskeyb6d4c2c2005-09-30 19:15:27 +0000745 } else {
746 switch (Node->getOpcode()) {
747 default:
Jim Laskey16d42c62006-07-11 18:25:13 +0000748#ifndef NDEBUG
Dan Gohmanb5bec2b2007-06-19 14:13:56 +0000749 Node->dump(&DAG);
Jim Laskey16d42c62006-07-11 18:25:13 +0000750#endif
Jim Laskeyb6d4c2c2005-09-30 19:15:27 +0000751 assert(0 && "This target-independent node should have been selected!");
752 case ISD::EntryToken: // fall thru
753 case ISD::TokenFactor:
Jim Laskey1ee29252007-01-26 14:34:52 +0000754 case ISD::LABEL:
Jim Laskeyb6d4c2c2005-09-30 19:15:27 +0000755 break;
756 case ISD::CopyToReg: {
Evan Cheng489a87c2007-01-05 20:59:06 +0000757 unsigned InReg;
758 if (RegisterSDNode *R = dyn_cast<RegisterSDNode>(Node->getOperand(2)))
759 InReg = R->getReg();
760 else
761 InReg = getVR(Node->getOperand(2), VRBaseMap);
Chris Lattnera4176522005-10-30 18:54:27 +0000762 unsigned DestReg = cast<RegisterSDNode>(Node->getOperand(1))->getReg();
Lauro Ramos Venancio8334b9f2007-03-20 16:46:44 +0000763 if (InReg != DestReg) {// Coalesced away the copy?
764 const TargetRegisterClass *TRC = 0;
765 // Get the target register class
Lauro Ramos Venancioa0a26b72007-03-20 20:09:03 +0000766 if (MRegisterInfo::isVirtualRegister(InReg))
Lauro Ramos Venancio8334b9f2007-03-20 16:46:44 +0000767 TRC = RegMap->getRegClass(InReg);
Lauro Ramos Venancioa0a26b72007-03-20 20:09:03 +0000768 else
769 TRC = getPhysicalRegisterRegClass(MRI,
770 Node->getOperand(2).getValueType(),
771 InReg);
Evan Cheng9efce632007-09-26 06:25:56 +0000772 MRI->copyRegToReg(*BB, BB->end(), DestReg, InReg, TRC, TRC);
Lauro Ramos Venancio8334b9f2007-03-20 16:46:44 +0000773 }
Jim Laskeyb6d4c2c2005-09-30 19:15:27 +0000774 break;
775 }
776 case ISD::CopyFromReg: {
777 unsigned SrcReg = cast<RegisterSDNode>(Node->getOperand(1))->getReg();
Evan Chenga6fb1b62007-09-25 01:54:36 +0000778 EmitCopyFromReg(Node, 0, InstanceNo, SrcReg, VRBaseMap);
Jim Laskeyb6d4c2c2005-09-30 19:15:27 +0000779 break;
780 }
Chris Lattneracc43bf2006-01-26 23:28:04 +0000781 case ISD::INLINEASM: {
782 unsigned NumOps = Node->getNumOperands();
783 if (Node->getOperand(NumOps-1).getValueType() == MVT::Flag)
784 --NumOps; // Ignore the flag operand.
785
786 // Create the inline asm machine instruction.
787 MachineInstr *MI =
Evan Chengc0f64ff2006-11-27 23:37:22 +0000788 new MachineInstr(BB, TII->get(TargetInstrInfo::INLINEASM));
Chris Lattneracc43bf2006-01-26 23:28:04 +0000789
790 // Add the asm string as an external symbol operand.
791 const char *AsmStr =
792 cast<ExternalSymbolSDNode>(Node->getOperand(1))->getSymbol();
Chris Lattnerea50fab2006-05-04 01:15:02 +0000793 MI->addExternalSymbolOperand(AsmStr);
Chris Lattneracc43bf2006-01-26 23:28:04 +0000794
795 // Add all of the operand registers to the instruction.
Chris Lattnerc3a9f8d2006-02-23 19:21:04 +0000796 for (unsigned i = 2; i != NumOps;) {
797 unsigned Flags = cast<ConstantSDNode>(Node->getOperand(i))->getValue();
Chris Lattnerfd6d2822006-02-24 19:18:20 +0000798 unsigned NumVals = Flags >> 3;
Chris Lattnerc3a9f8d2006-02-23 19:21:04 +0000799
Chris Lattner2d90ac72006-05-04 18:05:43 +0000800 MI->addImmOperand(Flags);
Chris Lattnerc3a9f8d2006-02-23 19:21:04 +0000801 ++i; // Skip the ID value.
802
803 switch (Flags & 7) {
Chris Lattneracc43bf2006-01-26 23:28:04 +0000804 default: assert(0 && "Bad flags!");
Chris Lattnerc3a9f8d2006-02-23 19:21:04 +0000805 case 1: // Use of register.
Chris Lattnerfd6d2822006-02-24 19:18:20 +0000806 for (; NumVals; --NumVals, ++i) {
Chris Lattnerc3a9f8d2006-02-23 19:21:04 +0000807 unsigned Reg = cast<RegisterSDNode>(Node->getOperand(i))->getReg();
Chris Lattner09e46062006-09-05 02:31:13 +0000808 MI->addRegOperand(Reg, false);
Chris Lattnerc3a9f8d2006-02-23 19:21:04 +0000809 }
Chris Lattnerdc19b702006-02-04 02:26:14 +0000810 break;
Chris Lattnerc3a9f8d2006-02-23 19:21:04 +0000811 case 2: // Def of register.
Chris Lattnerfd6d2822006-02-24 19:18:20 +0000812 for (; NumVals; --NumVals, ++i) {
Chris Lattnerc3a9f8d2006-02-23 19:21:04 +0000813 unsigned Reg = cast<RegisterSDNode>(Node->getOperand(i))->getReg();
Chris Lattner09e46062006-09-05 02:31:13 +0000814 MI->addRegOperand(Reg, true);
Chris Lattnerc3a9f8d2006-02-23 19:21:04 +0000815 }
Chris Lattnerdc19b702006-02-04 02:26:14 +0000816 break;
Chris Lattnerdc19b702006-02-04 02:26:14 +0000817 case 3: { // Immediate.
Chris Lattner7df31dc2007-08-25 00:53:07 +0000818 for (; NumVals; --NumVals, ++i) {
819 if (ConstantSDNode *CS =
820 dyn_cast<ConstantSDNode>(Node->getOperand(i))) {
821 MI->addImmOperand(CS->getValue());
822 } else {
823 GlobalAddressSDNode *GA =
824 cast<GlobalAddressSDNode>(Node->getOperand(i));
825 MI->addGlobalAddressOperand(GA->getGlobal(), GA->getOffset());
826 }
Chris Lattnerefa46ce2006-10-31 20:01:56 +0000827 }
Chris Lattnerdc19b702006-02-04 02:26:14 +0000828 break;
829 }
Chris Lattnerfd6d2822006-02-24 19:18:20 +0000830 case 4: // Addressing mode.
831 // The addressing mode has been selected, just add all of the
832 // operands to the machine instruction.
833 for (; NumVals; --NumVals, ++i)
Chris Lattnerdf375062006-03-10 07:25:12 +0000834 AddOperand(MI, Node->getOperand(i), 0, 0, VRBaseMap);
Chris Lattnerfd6d2822006-02-24 19:18:20 +0000835 break;
Chris Lattnerdc19b702006-02-04 02:26:14 +0000836 }
Chris Lattneracc43bf2006-01-26 23:28:04 +0000837 }
838 break;
839 }
Jim Laskeyb6d4c2c2005-09-30 19:15:27 +0000840 }
841 }
Jim Laskeyb6d4c2c2005-09-30 19:15:27 +0000842}
843
Chris Lattnera93dfcd2006-03-05 23:51:47 +0000844void ScheduleDAG::EmitNoop() {
845 TII->insertNoop(*BB, BB->end());
846}
847
Evan Chenge165a782006-05-11 23:55:42 +0000848/// EmitSchedule - Emit the machine code in scheduled order.
849void ScheduleDAG::EmitSchedule() {
Chris Lattner96645412006-05-16 06:10:58 +0000850 // If this is the first basic block in the function, and if it has live ins
851 // that need to be copied into vregs, emit the copies into the top of the
852 // block before emitting the code for the block.
853 MachineFunction &MF = DAG.getMachineFunction();
854 if (&MF.front() == BB && MF.livein_begin() != MF.livein_end()) {
855 for (MachineFunction::livein_iterator LI = MF.livein_begin(),
856 E = MF.livein_end(); LI != E; ++LI)
Evan Cheng9efce632007-09-26 06:25:56 +0000857 if (LI->second) {
858 const TargetRegisterClass *RC = RegMap->getRegClass(LI->second);
Chris Lattner96645412006-05-16 06:10:58 +0000859 MRI->copyRegToReg(*MF.begin(), MF.begin()->end(), LI->second,
Evan Cheng9efce632007-09-26 06:25:56 +0000860 LI->first, RC, RC);
861 }
Chris Lattner96645412006-05-16 06:10:58 +0000862 }
863
864
865 // Finally, emit the code for all of the scheduled instructions.
Evan Chengaf825c82007-07-10 07:08:32 +0000866 DenseMap<SDOperand, unsigned> VRBaseMap;
Evan Chenge165a782006-05-11 23:55:42 +0000867 for (unsigned i = 0, e = Sequence.size(); i != e; i++) {
868 if (SUnit *SU = Sequence[i]) {
Evan Chenga6fb1b62007-09-25 01:54:36 +0000869 for (unsigned j = 0, ee = SU->FlaggedNodes.size(); j != ee; ++j)
870 EmitNode(SU->FlaggedNodes[j], SU->InstanceNo, VRBaseMap);
871 EmitNode(SU->Node, SU->InstanceNo, VRBaseMap);
Evan Chenge165a782006-05-11 23:55:42 +0000872 } else {
873 // Null SUnit* is a noop.
874 EmitNoop();
875 }
876 }
877}
878
879/// dump - dump the schedule.
880void ScheduleDAG::dumpSchedule() const {
881 for (unsigned i = 0, e = Sequence.size(); i != e; i++) {
882 if (SUnit *SU = Sequence[i])
883 SU->dump(&DAG);
884 else
Bill Wendling832171c2006-12-07 20:04:42 +0000885 cerr << "**** NOOP ****\n";
Evan Chenge165a782006-05-11 23:55:42 +0000886 }
887}
888
889
Evan Chenga9c20912006-01-21 02:32:06 +0000890/// Run - perform scheduling.
891///
892MachineBasicBlock *ScheduleDAG::Run() {
893 TII = TM.getInstrInfo();
894 MRI = TM.getRegisterInfo();
895 RegMap = BB->getParent()->getSSARegMap();
896 ConstPool = BB->getParent()->getConstantPool();
Evan Cheng4ef10862006-01-23 07:01:07 +0000897
Evan Chenga9c20912006-01-21 02:32:06 +0000898 Schedule();
899 return BB;
Chris Lattnerd32b2362005-08-18 18:45:24 +0000900}
Evan Cheng4ef10862006-01-23 07:01:07 +0000901
Evan Chenge165a782006-05-11 23:55:42 +0000902/// SUnit - Scheduling unit. It's an wrapper around either a single SDNode or
903/// a group of nodes flagged together.
904void SUnit::dump(const SelectionDAG *G) const {
Bill Wendling832171c2006-12-07 20:04:42 +0000905 cerr << "SU(" << NodeNum << "): ";
Evan Chenge165a782006-05-11 23:55:42 +0000906 Node->dump(G);
Bill Wendling832171c2006-12-07 20:04:42 +0000907 cerr << "\n";
Evan Chenge165a782006-05-11 23:55:42 +0000908 if (FlaggedNodes.size() != 0) {
909 for (unsigned i = 0, e = FlaggedNodes.size(); i != e; i++) {
Bill Wendling832171c2006-12-07 20:04:42 +0000910 cerr << " ";
Evan Chenge165a782006-05-11 23:55:42 +0000911 FlaggedNodes[i]->dump(G);
Bill Wendling832171c2006-12-07 20:04:42 +0000912 cerr << "\n";
Evan Chenge165a782006-05-11 23:55:42 +0000913 }
914 }
915}
Evan Cheng4ef10862006-01-23 07:01:07 +0000916
Evan Chenge165a782006-05-11 23:55:42 +0000917void SUnit::dumpAll(const SelectionDAG *G) const {
918 dump(G);
919
Bill Wendling832171c2006-12-07 20:04:42 +0000920 cerr << " # preds left : " << NumPredsLeft << "\n";
921 cerr << " # succs left : " << NumSuccsLeft << "\n";
922 cerr << " # chain preds left : " << NumChainPredsLeft << "\n";
923 cerr << " # chain succs left : " << NumChainSuccsLeft << "\n";
924 cerr << " Latency : " << Latency << "\n";
925 cerr << " Depth : " << Depth << "\n";
926 cerr << " Height : " << Height << "\n";
Evan Chenge165a782006-05-11 23:55:42 +0000927
928 if (Preds.size() != 0) {
Bill Wendling832171c2006-12-07 20:04:42 +0000929 cerr << " Predecessors:\n";
Chris Lattner228a18e2006-08-17 00:09:56 +0000930 for (SUnit::const_succ_iterator I = Preds.begin(), E = Preds.end();
931 I != E; ++I) {
Evan Cheng713a98d2007-09-19 01:38:40 +0000932 if (I->isCtrl)
Bill Wendling832171c2006-12-07 20:04:42 +0000933 cerr << " ch #";
Evan Chenge165a782006-05-11 23:55:42 +0000934 else
Bill Wendling832171c2006-12-07 20:04:42 +0000935 cerr << " val #";
Evan Chenga6fb1b62007-09-25 01:54:36 +0000936 cerr << I->Dep << " - SU(" << I->Dep->NodeNum << ")";
937 if (I->isSpecial)
938 cerr << " *";
939 cerr << "\n";
Evan Chenge165a782006-05-11 23:55:42 +0000940 }
941 }
942 if (Succs.size() != 0) {
Bill Wendling832171c2006-12-07 20:04:42 +0000943 cerr << " Successors:\n";
Chris Lattner228a18e2006-08-17 00:09:56 +0000944 for (SUnit::const_succ_iterator I = Succs.begin(), E = Succs.end();
945 I != E; ++I) {
Evan Cheng713a98d2007-09-19 01:38:40 +0000946 if (I->isCtrl)
Bill Wendling832171c2006-12-07 20:04:42 +0000947 cerr << " ch #";
Evan Chenge165a782006-05-11 23:55:42 +0000948 else
Bill Wendling832171c2006-12-07 20:04:42 +0000949 cerr << " val #";
Evan Chenga6fb1b62007-09-25 01:54:36 +0000950 cerr << I->Dep << " - SU(" << I->Dep->NodeNum << ")";
951 if (I->isSpecial)
952 cerr << " *";
953 cerr << "\n";
Evan Chenge165a782006-05-11 23:55:42 +0000954 }
955 }
Bill Wendling832171c2006-12-07 20:04:42 +0000956 cerr << "\n";
Evan Chenge165a782006-05-11 23:55:42 +0000957}