blob: 53fec718ff29388cf65ee04bc2f958361d6c6a4b [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//
Chris Lattner4ee451d2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Chris Lattnerd32b2362005-08-18 18:45:24 +00007//
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;
Evan Cheng22a52992007-09-28 22:32:30 +000066 SU->hasPhysRegDefs = Old->hasPhysRegDefs;
Evan Chenga6fb1b62007-09-25 01:54:36 +000067 SUnitMap[Old->Node].push_back(SU);
68 return SU;
69}
70
Evan Chengf10c9732007-10-05 01:39:18 +000071
Evan Chenge165a782006-05-11 23:55:42 +000072/// BuildSchedUnits - Build SUnits from the selection dag that we are input.
73/// This SUnit graph is similar to the SelectionDAG, but represents flagged
74/// together nodes with a single SUnit.
75void ScheduleDAG::BuildSchedUnits() {
76 // Reserve entries in the vector for each of the SUnits we are creating. This
77 // ensure that reallocation of the vector won't happen, so SUnit*'s won't get
78 // invalidated.
79 SUnits.reserve(std::distance(DAG.allnodes_begin(), DAG.allnodes_end()));
80
Evan Chenge165a782006-05-11 23:55:42 +000081 for (SelectionDAG::allnodes_iterator NI = DAG.allnodes_begin(),
82 E = DAG.allnodes_end(); NI != E; ++NI) {
83 if (isPassiveNode(NI)) // Leaf node, e.g. a TargetImmediate.
84 continue;
85
86 // If this node has already been processed, stop now.
Evan Chenga6fb1b62007-09-25 01:54:36 +000087 if (SUnitMap[NI].size()) continue;
Evan Chenge165a782006-05-11 23:55:42 +000088
89 SUnit *NodeSUnit = NewSUnit(NI);
90
91 // See if anything is flagged to this node, if so, add them to flagged
92 // nodes. Nodes can have at most one flag input and one flag output. Flags
93 // are required the be the last operand and result of a node.
94
95 // Scan up, adding flagged preds to FlaggedNodes.
96 SDNode *N = NI;
Evan Cheng3b97acd2006-08-07 22:12:12 +000097 if (N->getNumOperands() &&
98 N->getOperand(N->getNumOperands()-1).getValueType() == MVT::Flag) {
99 do {
100 N = N->getOperand(N->getNumOperands()-1).Val;
101 NodeSUnit->FlaggedNodes.push_back(N);
Evan Chenga6fb1b62007-09-25 01:54:36 +0000102 SUnitMap[N].push_back(NodeSUnit);
Evan Cheng3b97acd2006-08-07 22:12:12 +0000103 } while (N->getNumOperands() &&
104 N->getOperand(N->getNumOperands()-1).getValueType()== MVT::Flag);
105 std::reverse(NodeSUnit->FlaggedNodes.begin(),
106 NodeSUnit->FlaggedNodes.end());
Evan Chenge165a782006-05-11 23:55:42 +0000107 }
108
109 // Scan down, adding this node and any flagged succs to FlaggedNodes if they
110 // have a user of the flag operand.
111 N = NI;
112 while (N->getValueType(N->getNumValues()-1) == MVT::Flag) {
113 SDOperand FlagVal(N, N->getNumValues()-1);
114
115 // There are either zero or one users of the Flag result.
116 bool HasFlagUse = false;
117 for (SDNode::use_iterator UI = N->use_begin(), E = N->use_end();
118 UI != E; ++UI)
119 if (FlagVal.isOperand(*UI)) {
120 HasFlagUse = true;
121 NodeSUnit->FlaggedNodes.push_back(N);
Evan Chenga6fb1b62007-09-25 01:54:36 +0000122 SUnitMap[N].push_back(NodeSUnit);
Evan Chenge165a782006-05-11 23:55:42 +0000123 N = *UI;
124 break;
125 }
Chris Lattner228a18e2006-08-17 00:09:56 +0000126 if (!HasFlagUse) break;
Evan Chenge165a782006-05-11 23:55:42 +0000127 }
128
129 // Now all flagged nodes are in FlaggedNodes and N is the bottom-most node.
130 // Update the SUnit
131 NodeSUnit->Node = N;
Evan Chenga6fb1b62007-09-25 01:54:36 +0000132 SUnitMap[N].push_back(NodeSUnit);
Evan Chengf10c9732007-10-05 01:39:18 +0000133
134 ComputeLatency(NodeSUnit);
Evan Chenge165a782006-05-11 23:55:42 +0000135 }
136
137 // Pass 2: add the preds, succs, etc.
138 for (unsigned su = 0, e = SUnits.size(); su != e; ++su) {
139 SUnit *SU = &SUnits[su];
140 SDNode *MainNode = SU->Node;
141
142 if (MainNode->isTargetOpcode()) {
143 unsigned Opc = MainNode->getTargetOpcode();
Evan Chenga6fb1b62007-09-25 01:54:36 +0000144 const TargetInstrDescriptor &TID = TII->get(Opc);
Evan Chenga6fb1b62007-09-25 01:54:36 +0000145 for (unsigned i = 0; i != TID.numOperands; ++i) {
146 if (TID.getOperandConstraint(i, TOI::TIED_TO) != -1) {
Evan Cheng95f6ede2006-11-04 09:44:31 +0000147 SU->isTwoAddress = true;
148 break;
149 }
150 }
Evan Chenga6fb1b62007-09-25 01:54:36 +0000151 if (TID.Flags & M_COMMUTABLE)
Evan Cheng13d41b92006-05-12 01:58:24 +0000152 SU->isCommutable = true;
Evan Chenge165a782006-05-11 23:55:42 +0000153 }
154
155 // Find all predecessors and successors of the group.
156 // Temporarily add N to make code simpler.
157 SU->FlaggedNodes.push_back(MainNode);
158
159 for (unsigned n = 0, e = SU->FlaggedNodes.size(); n != e; ++n) {
160 SDNode *N = SU->FlaggedNodes[n];
Evan Cheng22a52992007-09-28 22:32:30 +0000161 if (N->isTargetOpcode() &&
162 TII->getImplicitDefs(N->getTargetOpcode()) &&
163 CountResults(N) > (unsigned)TII->getNumDefs(N->getTargetOpcode()))
164 SU->hasPhysRegDefs = true;
Evan Chenge165a782006-05-11 23:55:42 +0000165
166 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
167 SDNode *OpN = N->getOperand(i).Val;
168 if (isPassiveNode(OpN)) continue; // Not scheduled.
Evan Chenga6fb1b62007-09-25 01:54:36 +0000169 SUnit *OpSU = SUnitMap[OpN].front();
Evan Chenge165a782006-05-11 23:55:42 +0000170 assert(OpSU && "Node has no SUnit!");
171 if (OpSU == SU) continue; // In the same group.
172
173 MVT::ValueType OpVT = N->getOperand(i).getValueType();
174 assert(OpVT != MVT::Flag && "Flagged nodes should be in same sunit!");
175 bool isChain = OpVT == MVT::Other;
Evan Chenga6fb1b62007-09-25 01:54:36 +0000176
177 unsigned PhysReg = 0;
178 int Cost = 1;
179 // Determine if this is a physical register dependency.
180 CheckForPhysRegDependency(OpN, N, i, MRI, TII, PhysReg, Cost);
181 SU->addPred(OpSU, isChain, false, PhysReg, Cost);
Evan Chenge165a782006-05-11 23:55:42 +0000182 }
183 }
184
185 // Remove MainNode from FlaggedNodes again.
186 SU->FlaggedNodes.pop_back();
187 }
188
189 return;
190}
191
Evan Chengf10c9732007-10-05 01:39:18 +0000192void ScheduleDAG::ComputeLatency(SUnit *SU) {
193 const InstrItineraryData &InstrItins = TM.getInstrItineraryData();
194
195 // Compute the latency for the node. We use the sum of the latencies for
196 // all nodes flagged together into this SUnit.
197 if (InstrItins.isEmpty()) {
198 // No latency information.
199 SU->Latency = 1;
200 } else {
201 SU->Latency = 0;
202 if (SU->Node->isTargetOpcode()) {
203 unsigned SchedClass = TII->getSchedClass(SU->Node->getTargetOpcode());
204 InstrStage *S = InstrItins.begin(SchedClass);
205 InstrStage *E = InstrItins.end(SchedClass);
206 for (; S != E; ++S)
207 SU->Latency += S->Cycles;
208 }
209 for (unsigned i = 0, e = SU->FlaggedNodes.size(); i != e; ++i) {
210 SDNode *FNode = SU->FlaggedNodes[i];
211 if (FNode->isTargetOpcode()) {
212 unsigned SchedClass = TII->getSchedClass(FNode->getTargetOpcode());
213 InstrStage *S = InstrItins.begin(SchedClass);
214 InstrStage *E = InstrItins.end(SchedClass);
215 for (; S != E; ++S)
216 SU->Latency += S->Cycles;
217 }
218 }
219 }
220}
221
Evan Chenge165a782006-05-11 23:55:42 +0000222void ScheduleDAG::CalculateDepths() {
Evan Cheng99126282007-07-06 01:37:28 +0000223 std::vector<std::pair<SUnit*, unsigned> > WorkList;
Evan Chenge165a782006-05-11 23:55:42 +0000224 for (unsigned i = 0, e = SUnits.size(); i != e; ++i)
Evan Cheng69001322007-09-12 23:45:46 +0000225 if (SUnits[i].Preds.size() == 0)
Evan Cheng99126282007-07-06 01:37:28 +0000226 WorkList.push_back(std::make_pair(&SUnits[i], 0U));
Evan Chenge165a782006-05-11 23:55:42 +0000227
Evan Cheng99126282007-07-06 01:37:28 +0000228 while (!WorkList.empty()) {
229 SUnit *SU = WorkList.back().first;
230 unsigned Depth = WorkList.back().second;
231 WorkList.pop_back();
232 if (SU->Depth == 0 || Depth > SU->Depth) {
233 SU->Depth = Depth;
234 for (SUnit::succ_iterator I = SU->Succs.begin(), E = SU->Succs.end();
235 I != E; ++I)
Evan Cheng713a98d2007-09-19 01:38:40 +0000236 WorkList.push_back(std::make_pair(I->Dep, Depth+1));
Evan Cheng99126282007-07-06 01:37:28 +0000237 }
Evan Cheng626da3d2006-05-12 06:05:18 +0000238 }
Evan Chenge165a782006-05-11 23:55:42 +0000239}
Evan Cheng99126282007-07-06 01:37:28 +0000240
Evan Chenge165a782006-05-11 23:55:42 +0000241void ScheduleDAG::CalculateHeights() {
Evan Cheng99126282007-07-06 01:37:28 +0000242 std::vector<std::pair<SUnit*, unsigned> > WorkList;
Evan Chenga6fb1b62007-09-25 01:54:36 +0000243 SUnit *Root = SUnitMap[DAG.getRoot().Val].front();
Evan Cheng99126282007-07-06 01:37:28 +0000244 WorkList.push_back(std::make_pair(Root, 0U));
245
246 while (!WorkList.empty()) {
247 SUnit *SU = WorkList.back().first;
248 unsigned Height = WorkList.back().second;
249 WorkList.pop_back();
250 if (SU->Height == 0 || Height > SU->Height) {
251 SU->Height = Height;
252 for (SUnit::pred_iterator I = SU->Preds.begin(), E = SU->Preds.end();
253 I != E; ++I)
Evan Cheng713a98d2007-09-19 01:38:40 +0000254 WorkList.push_back(std::make_pair(I->Dep, Height+1));
Evan Cheng99126282007-07-06 01:37:28 +0000255 }
256 }
Evan Chenge165a782006-05-11 23:55:42 +0000257}
258
Jim Laskeye6b90fb2005-09-26 21:57:04 +0000259/// CountResults - The results of target nodes have register or immediate
260/// operands first, then an optional chain, and optional flag operands (which do
261/// not go into the machine instrs.)
Evan Cheng95f6ede2006-11-04 09:44:31 +0000262unsigned ScheduleDAG::CountResults(SDNode *Node) {
Jim Laskeyb6d4c2c2005-09-30 19:15:27 +0000263 unsigned N = Node->getNumValues();
264 while (N && Node->getValueType(N - 1) == MVT::Flag)
Jim Laskeye6b90fb2005-09-26 21:57:04 +0000265 --N;
Jim Laskeyb6d4c2c2005-09-30 19:15:27 +0000266 if (N && Node->getValueType(N - 1) == MVT::Other)
Jim Laskeye6b90fb2005-09-26 21:57:04 +0000267 --N; // Skip over chain result.
268 return N;
269}
270
271/// CountOperands The inputs to target nodes have any actual inputs first,
272/// followed by an optional chain operand, then flag operands. Compute the
273/// number of actual operands that will go into the machine instr.
Evan Cheng95f6ede2006-11-04 09:44:31 +0000274unsigned ScheduleDAG::CountOperands(SDNode *Node) {
Jim Laskeyb6d4c2c2005-09-30 19:15:27 +0000275 unsigned N = Node->getNumOperands();
276 while (N && Node->getOperand(N - 1).getValueType() == MVT::Flag)
Jim Laskeye6b90fb2005-09-26 21:57:04 +0000277 --N;
Jim Laskeyb6d4c2c2005-09-30 19:15:27 +0000278 if (N && Node->getOperand(N - 1).getValueType() == MVT::Other)
Jim Laskeye6b90fb2005-09-26 21:57:04 +0000279 --N; // Ignore chain if it exists.
280 return N;
281}
282
Jim Laskey60f09922006-07-21 20:57:35 +0000283static const TargetRegisterClass *getInstrOperandRegClass(
284 const MRegisterInfo *MRI,
285 const TargetInstrInfo *TII,
286 const TargetInstrDescriptor *II,
287 unsigned Op) {
288 if (Op >= II->numOperands) {
289 assert((II->Flags & M_VARIABLE_OPS)&& "Invalid operand # of instruction");
290 return NULL;
291 }
292 const TargetOperandInfo &toi = II->OpInfo[Op];
293 return (toi.Flags & M_LOOK_UP_PTR_REG_CLASS)
294 ? TII->getPointerRegClass() : MRI->getRegClass(toi.RegClass);
295}
296
Evan Chenga6fb1b62007-09-25 01:54:36 +0000297void ScheduleDAG::EmitCopyFromReg(SDNode *Node, unsigned ResNo,
298 unsigned InstanceNo, unsigned SrcReg,
Evan Cheng84097472007-08-02 00:28:15 +0000299 DenseMap<SDOperand, unsigned> &VRBaseMap) {
300 unsigned VRBase = 0;
301 if (MRegisterInfo::isVirtualRegister(SrcReg)) {
302 // Just use the input register directly!
Evan Chenga6fb1b62007-09-25 01:54:36 +0000303 if (InstanceNo > 0)
304 VRBaseMap.erase(SDOperand(Node, ResNo));
Evan Cheng84097472007-08-02 00:28:15 +0000305 bool isNew = VRBaseMap.insert(std::make_pair(SDOperand(Node,ResNo),SrcReg));
306 assert(isNew && "Node emitted out of order - early");
307 return;
308 }
309
310 // If the node is only used by a CopyToReg and the dest reg is a vreg, use
311 // the CopyToReg'd destination register instead of creating a new vreg.
Evan Chenga6fb1b62007-09-25 01:54:36 +0000312 bool MatchReg = true;
Evan Cheng84097472007-08-02 00:28:15 +0000313 for (SDNode::use_iterator UI = Node->use_begin(), E = Node->use_end();
314 UI != E; ++UI) {
315 SDNode *Use = *UI;
Evan Chenga6fb1b62007-09-25 01:54:36 +0000316 bool Match = true;
Evan Cheng84097472007-08-02 00:28:15 +0000317 if (Use->getOpcode() == ISD::CopyToReg &&
318 Use->getOperand(2).Val == Node &&
319 Use->getOperand(2).ResNo == ResNo) {
320 unsigned DestReg = cast<RegisterSDNode>(Use->getOperand(1))->getReg();
321 if (MRegisterInfo::isVirtualRegister(DestReg)) {
322 VRBase = DestReg;
Evan Chenga6fb1b62007-09-25 01:54:36 +0000323 Match = false;
324 } else if (DestReg != SrcReg)
325 Match = false;
326 } else {
327 for (unsigned i = 0, e = Use->getNumOperands(); i != e; ++i) {
328 SDOperand Op = Use->getOperand(i);
Evan Cheng7c07aeb2007-12-14 08:25:15 +0000329 if (Op.Val != Node || Op.ResNo != ResNo)
Evan Chenga6fb1b62007-09-25 01:54:36 +0000330 continue;
331 MVT::ValueType VT = Node->getValueType(Op.ResNo);
332 if (VT != MVT::Other && VT != MVT::Flag)
333 Match = false;
Evan Cheng84097472007-08-02 00:28:15 +0000334 }
335 }
Evan Chenga6fb1b62007-09-25 01:54:36 +0000336 MatchReg &= Match;
337 if (VRBase)
338 break;
Evan Cheng84097472007-08-02 00:28:15 +0000339 }
340
Evan Cheng84097472007-08-02 00:28:15 +0000341 const TargetRegisterClass *TRC = 0;
Evan Chenga6fb1b62007-09-25 01:54:36 +0000342 // Figure out the register class to create for the destreg.
343 if (VRBase)
Evan Cheng84097472007-08-02 00:28:15 +0000344 TRC = RegMap->getRegClass(VRBase);
Evan Chenga6fb1b62007-09-25 01:54:36 +0000345 else
Evan Cheng42d60272007-09-26 21:36:17 +0000346 TRC = MRI->getPhysicalRegisterRegClass(Node->getValueType(ResNo), SrcReg);
Evan Chenga6fb1b62007-09-25 01:54:36 +0000347
348 // If all uses are reading from the src physical register and copying the
349 // register is either impossible or very expensive, then don't create a copy.
350 if (MatchReg && TRC->getCopyCost() < 0) {
351 VRBase = SrcReg;
352 } else {
Evan Cheng84097472007-08-02 00:28:15 +0000353 // Create the reg, emit the copy.
354 VRBase = RegMap->createVirtualRegister(TRC);
Evan Cheng9efce632007-09-26 06:25:56 +0000355 MRI->copyRegToReg(*BB, BB->end(), VRBase, SrcReg, TRC, TRC);
Evan Cheng84097472007-08-02 00:28:15 +0000356 }
Evan Cheng84097472007-08-02 00:28:15 +0000357
Evan Chenga6fb1b62007-09-25 01:54:36 +0000358 if (InstanceNo > 0)
359 VRBaseMap.erase(SDOperand(Node, ResNo));
Evan Cheng84097472007-08-02 00:28:15 +0000360 bool isNew = VRBaseMap.insert(std::make_pair(SDOperand(Node,ResNo), VRBase));
361 assert(isNew && "Node emitted out of order - early");
362}
363
364void ScheduleDAG::CreateVirtualRegisters(SDNode *Node,
365 MachineInstr *MI,
366 const TargetInstrDescriptor &II,
367 DenseMap<SDOperand, unsigned> &VRBaseMap) {
368 for (unsigned i = 0; i < II.numDefs; ++i) {
Evan Chengaf825c82007-07-10 07:08:32 +0000369 // If the specific node value is only used by a CopyToReg and the dest reg
370 // is a vreg, use the CopyToReg'd destination register instead of creating
371 // a new vreg.
372 unsigned VRBase = 0;
373 for (SDNode::use_iterator UI = Node->use_begin(), E = Node->use_end();
374 UI != E; ++UI) {
375 SDNode *Use = *UI;
376 if (Use->getOpcode() == ISD::CopyToReg &&
377 Use->getOperand(2).Val == Node &&
378 Use->getOperand(2).ResNo == i) {
379 unsigned Reg = cast<RegisterSDNode>(Use->getOperand(1))->getReg();
380 if (MRegisterInfo::isVirtualRegister(Reg)) {
381 VRBase = Reg;
Chris Lattner8019f412007-12-30 00:41:17 +0000382 MI->addOperand(MachineOperand::CreateReg(Reg, true));
Evan Chengaf825c82007-07-10 07:08:32 +0000383 break;
384 }
385 }
386 }
387
Evan Cheng84097472007-08-02 00:28:15 +0000388 // Create the result registers for this node and add the result regs to
389 // the machine instruction.
Evan Chengaf825c82007-07-10 07:08:32 +0000390 if (VRBase == 0) {
Evan Chengaf825c82007-07-10 07:08:32 +0000391 const TargetRegisterClass *RC = getInstrOperandRegClass(MRI, TII, &II, i);
392 assert(RC && "Isn't a register operand!");
393 VRBase = RegMap->createVirtualRegister(RC);
Chris Lattner8019f412007-12-30 00:41:17 +0000394 MI->addOperand(MachineOperand::CreateReg(VRBase, true));
Evan Chengaf825c82007-07-10 07:08:32 +0000395 }
396
397 bool isNew = VRBaseMap.insert(std::make_pair(SDOperand(Node,i), VRBase));
398 assert(isNew && "Node emitted out of order - early");
Jim Laskeye6b90fb2005-09-26 21:57:04 +0000399 }
Jim Laskeye6b90fb2005-09-26 21:57:04 +0000400}
401
Chris Lattnerdf375062006-03-10 07:25:12 +0000402/// getVR - Return the virtual register corresponding to the specified result
403/// of the specified node.
Evan Chengaf825c82007-07-10 07:08:32 +0000404static unsigned getVR(SDOperand Op, DenseMap<SDOperand, unsigned> &VRBaseMap) {
405 DenseMap<SDOperand, unsigned>::iterator I = VRBaseMap.find(Op);
Chris Lattnerdf375062006-03-10 07:25:12 +0000406 assert(I != VRBaseMap.end() && "Node emitted out of order - late");
Evan Chengaf825c82007-07-10 07:08:32 +0000407 return I->second;
Chris Lattnerdf375062006-03-10 07:25:12 +0000408}
409
410
Chris Lattnered18b682006-02-24 18:54:03 +0000411/// AddOperand - Add the specified operand to the specified machine instr. II
412/// specifies the instruction information for the node, and IIOpNum is the
413/// operand number (in the II) that we are adding. IIOpNum and II are used for
414/// assertions only.
415void ScheduleDAG::AddOperand(MachineInstr *MI, SDOperand Op,
416 unsigned IIOpNum,
Chris Lattnerdf375062006-03-10 07:25:12 +0000417 const TargetInstrDescriptor *II,
Evan Chengaf825c82007-07-10 07:08:32 +0000418 DenseMap<SDOperand, unsigned> &VRBaseMap) {
Chris Lattnered18b682006-02-24 18:54:03 +0000419 if (Op.isTargetOpcode()) {
420 // Note that this case is redundant with the final else block, but we
421 // include it because it is the most common and it makes the logic
422 // simpler here.
423 assert(Op.getValueType() != MVT::Other &&
424 Op.getValueType() != MVT::Flag &&
425 "Chain and flag operands should occur at end of operand list!");
426
427 // Get/emit the operand.
Chris Lattnerdf375062006-03-10 07:25:12 +0000428 unsigned VReg = getVR(Op, VRBaseMap);
Evan Cheng5e2456c2007-07-10 17:52:20 +0000429 const TargetInstrDescriptor *TID = MI->getInstrDescriptor();
430 bool isOptDef = (IIOpNum < TID->numOperands)
431 ? (TID->OpInfo[IIOpNum].Flags & M_OPTIONAL_DEF_OPERAND) : false;
Chris Lattner8019f412007-12-30 00:41:17 +0000432 MI->addOperand(MachineOperand::CreateReg(VReg, isOptDef));
Chris Lattnered18b682006-02-24 18:54:03 +0000433
434 // Verify that it is right.
435 assert(MRegisterInfo::isVirtualRegister(VReg) && "Not a vreg?");
436 if (II) {
Jim Laskey60f09922006-07-21 20:57:35 +0000437 const TargetRegisterClass *RC =
438 getInstrOperandRegClass(MRI, TII, II, IIOpNum);
Evan Cheng21d03f22006-05-18 20:42:07 +0000439 assert(RC && "Don't have operand info for this instruction!");
Chris Lattner01528292007-02-15 18:17:56 +0000440 const TargetRegisterClass *VRC = RegMap->getRegClass(VReg);
441 if (VRC != RC) {
442 cerr << "Register class of operand and regclass of use don't agree!\n";
443#ifndef NDEBUG
444 cerr << "Operand = " << IIOpNum << "\n";
Chris Lattner95ad9432007-02-17 06:38:37 +0000445 cerr << "Op->Val = "; Op.Val->dump(&DAG); cerr << "\n";
Chris Lattner01528292007-02-15 18:17:56 +0000446 cerr << "MI = "; MI->print(cerr);
447 cerr << "VReg = " << VReg << "\n";
448 cerr << "VReg RegClass size = " << VRC->getSize()
Chris Lattner5d4a9f72007-02-15 18:19:15 +0000449 << ", align = " << VRC->getAlignment() << "\n";
Chris Lattner01528292007-02-15 18:17:56 +0000450 cerr << "Expected RegClass size = " << RC->getSize()
Chris Lattner5d4a9f72007-02-15 18:19:15 +0000451 << ", align = " << RC->getAlignment() << "\n";
Chris Lattner01528292007-02-15 18:17:56 +0000452#endif
453 cerr << "Fatal error, aborting.\n";
454 abort();
455 }
Chris Lattnered18b682006-02-24 18:54:03 +0000456 }
Chris Lattnerfec65d52007-12-30 00:51:11 +0000457 } else if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Chris Lattner8019f412007-12-30 00:41:17 +0000458 MI->addOperand(MachineOperand::CreateImm(C->getValue()));
Chris Lattnerfec65d52007-12-30 00:51:11 +0000459 } else if (RegisterSDNode *R = dyn_cast<RegisterSDNode>(Op)) {
Chris Lattner8019f412007-12-30 00:41:17 +0000460 MI->addOperand(MachineOperand::CreateReg(R->getReg(), false));
Chris Lattnerfec65d52007-12-30 00:51:11 +0000461 } else if (GlobalAddressSDNode *TGA = dyn_cast<GlobalAddressSDNode>(Op)) {
462 MI->addOperand(MachineOperand::CreateGA(TGA->getGlobal(),TGA->getOffset()));
463 } else if (BasicBlockSDNode *BB = dyn_cast<BasicBlockSDNode>(Op)) {
464 MI->addOperand(MachineOperand::CreateMBB(BB->getBasicBlock()));
465 } else if (FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(Op)) {
466 MI->addOperand(MachineOperand::CreateFI(FI->getIndex()));
467 } else if (JumpTableSDNode *JT = dyn_cast<JumpTableSDNode>(Op)) {
468 MI->addOperand(MachineOperand::CreateJTI(JT->getIndex()));
469 } else if (ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(Op)) {
Evan Cheng404cb4f2006-02-25 09:54:52 +0000470 int Offset = CP->getOffset();
Chris Lattnered18b682006-02-24 18:54:03 +0000471 unsigned Align = CP->getAlignment();
Evan Chengd6594ae2006-09-12 21:00:35 +0000472 const Type *Type = CP->getType();
Chris Lattnered18b682006-02-24 18:54:03 +0000473 // MachineConstantPool wants an explicit alignment.
474 if (Align == 0) {
Evan Chengde268f72007-01-24 07:03:39 +0000475 Align = TM.getTargetData()->getPreferredTypeAlignmentShift(Type);
Evan Chengf6d039a2007-01-22 23:13:55 +0000476 if (Align == 0) {
Reid Spencerac9dcb92007-02-15 03:39:18 +0000477 // Alignment of vector types. FIXME!
Duncan Sands514ab342007-11-01 20:53:16 +0000478 Align = TM.getTargetData()->getABITypeSize(Type);
Evan Chengf6d039a2007-01-22 23:13:55 +0000479 Align = Log2_64(Align);
Chris Lattner54a30b92006-03-20 01:51:46 +0000480 }
Chris Lattnered18b682006-02-24 18:54:03 +0000481 }
482
Evan Chengd6594ae2006-09-12 21:00:35 +0000483 unsigned Idx;
484 if (CP->isMachineConstantPoolEntry())
485 Idx = ConstPool->getConstantPoolIndex(CP->getMachineCPVal(), Align);
486 else
487 Idx = ConstPool->getConstantPoolIndex(CP->getConstVal(), Align);
Chris Lattnerfec65d52007-12-30 00:51:11 +0000488 MI->addOperand(MachineOperand::CreateCPI(Idx, Offset));
489 } else if (ExternalSymbolSDNode *ES = dyn_cast<ExternalSymbolSDNode>(Op)) {
490 MI->addOperand(MachineOperand::CreateES(ES->getSymbol()));
Chris Lattnered18b682006-02-24 18:54:03 +0000491 } else {
492 assert(Op.getValueType() != MVT::Other &&
493 Op.getValueType() != MVT::Flag &&
494 "Chain and flag operands should occur at end of operand list!");
Chris Lattnerdf375062006-03-10 07:25:12 +0000495 unsigned VReg = getVR(Op, VRBaseMap);
Chris Lattner8019f412007-12-30 00:41:17 +0000496 MI->addOperand(MachineOperand::CreateReg(VReg, false));
Chris Lattnered18b682006-02-24 18:54:03 +0000497
498 // Verify that it is right.
499 assert(MRegisterInfo::isVirtualRegister(VReg) && "Not a vreg?");
500 if (II) {
Jim Laskey60f09922006-07-21 20:57:35 +0000501 const TargetRegisterClass *RC =
502 getInstrOperandRegClass(MRI, TII, II, IIOpNum);
Evan Cheng21d03f22006-05-18 20:42:07 +0000503 assert(RC && "Don't have operand info for this instruction!");
504 assert(RegMap->getRegClass(VReg) == RC &&
Chris Lattnered18b682006-02-24 18:54:03 +0000505 "Register class of operand and regclass of use don't agree!");
506 }
507 }
508
509}
510
Christopher Lambe24f8f12007-07-26 08:12:07 +0000511// Returns the Register Class of a subregister
512static const TargetRegisterClass *getSubRegisterRegClass(
513 const TargetRegisterClass *TRC,
514 unsigned SubIdx) {
515 // Pick the register class of the subregister
516 MRegisterInfo::regclass_iterator I = TRC->subregclasses_begin() + SubIdx-1;
517 assert(I < TRC->subregclasses_end() &&
518 "Invalid subregister index for register class");
519 return *I;
520}
521
522static const TargetRegisterClass *getSuperregRegisterClass(
523 const TargetRegisterClass *TRC,
524 unsigned SubIdx,
525 MVT::ValueType VT) {
526 // Pick the register class of the superegister for this type
527 for (MRegisterInfo::regclass_iterator I = TRC->superregclasses_begin(),
528 E = TRC->superregclasses_end(); I != E; ++I)
529 if ((*I)->hasType(VT) && getSubRegisterRegClass(*I, SubIdx) == TRC)
530 return *I;
531 assert(false && "Couldn't find the register class");
532 return 0;
533}
534
535/// EmitSubregNode - Generate machine code for subreg nodes.
536///
537void ScheduleDAG::EmitSubregNode(SDNode *Node,
538 DenseMap<SDOperand, unsigned> &VRBaseMap) {
539 unsigned VRBase = 0;
540 unsigned Opc = Node->getTargetOpcode();
541 if (Opc == TargetInstrInfo::EXTRACT_SUBREG) {
542 // If the node is only used by a CopyToReg and the dest reg is a vreg, use
543 // the CopyToReg'd destination register instead of creating a new vreg.
544 for (SDNode::use_iterator UI = Node->use_begin(), E = Node->use_end();
545 UI != E; ++UI) {
546 SDNode *Use = *UI;
547 if (Use->getOpcode() == ISD::CopyToReg &&
548 Use->getOperand(2).Val == Node) {
549 unsigned DestReg = cast<RegisterSDNode>(Use->getOperand(1))->getReg();
550 if (MRegisterInfo::isVirtualRegister(DestReg)) {
551 VRBase = DestReg;
552 break;
553 }
554 }
555 }
556
557 unsigned SubIdx = cast<ConstantSDNode>(Node->getOperand(1))->getValue();
558
559 // TODO: If the node is a use of a CopyFromReg from a physical register
560 // fold the extract into the copy now
561
Christopher Lambe24f8f12007-07-26 08:12:07 +0000562 // Create the extract_subreg machine instruction.
563 MachineInstr *MI =
564 new MachineInstr(BB, TII->get(TargetInstrInfo::EXTRACT_SUBREG));
565
566 // Figure out the register class to create for the destreg.
567 unsigned VReg = getVR(Node->getOperand(0), VRBaseMap);
568 const TargetRegisterClass *TRC = RegMap->getRegClass(VReg);
569 const TargetRegisterClass *SRC = getSubRegisterRegClass(TRC, SubIdx);
570
571 if (VRBase) {
572 // Grab the destination register
573 const TargetRegisterClass *DRC = 0;
574 DRC = RegMap->getRegClass(VRBase);
575 assert(SRC == DRC &&
576 "Source subregister and destination must have the same class");
577 } else {
578 // Create the reg
579 VRBase = RegMap->createVirtualRegister(SRC);
580 }
581
582 // Add def, source, and subreg index
Chris Lattner8019f412007-12-30 00:41:17 +0000583 MI->addOperand(MachineOperand::CreateReg(VRBase, true));
Christopher Lambe24f8f12007-07-26 08:12:07 +0000584 AddOperand(MI, Node->getOperand(0), 0, 0, VRBaseMap);
Chris Lattnerfec65d52007-12-30 00:51:11 +0000585 MI->addOperand(MachineOperand::CreateImm(SubIdx));
Christopher Lambe24f8f12007-07-26 08:12:07 +0000586
587 } else if (Opc == TargetInstrInfo::INSERT_SUBREG) {
588 assert((Node->getNumOperands() == 2 || Node->getNumOperands() == 3) &&
589 "Malformed insert_subreg node");
590 bool isUndefInput = (Node->getNumOperands() == 2);
591 unsigned SubReg = 0;
592 unsigned SubIdx = 0;
593
594 if (isUndefInput) {
595 SubReg = getVR(Node->getOperand(0), VRBaseMap);
596 SubIdx = cast<ConstantSDNode>(Node->getOperand(1))->getValue();
597 } else {
598 SubReg = getVR(Node->getOperand(1), VRBaseMap);
599 SubIdx = cast<ConstantSDNode>(Node->getOperand(2))->getValue();
600 }
601
602 // TODO: Add tracking info to SSARegMap of which vregs are subregs
603 // to allow coalescing in the allocator
604
605 // If the node is only used by a CopyToReg and the dest reg is a vreg, use
606 // the CopyToReg'd destination register instead of creating a new vreg.
607 // If the CopyToReg'd destination register is physical, then fold the
608 // insert into the copy
609 for (SDNode::use_iterator UI = Node->use_begin(), E = Node->use_end();
610 UI != E; ++UI) {
611 SDNode *Use = *UI;
612 if (Use->getOpcode() == ISD::CopyToReg &&
613 Use->getOperand(2).Val == Node) {
614 unsigned DestReg = cast<RegisterSDNode>(Use->getOperand(1))->getReg();
615 if (MRegisterInfo::isVirtualRegister(DestReg)) {
616 VRBase = DestReg;
617 break;
618 }
619 }
620 }
621
622 // Create the insert_subreg machine instruction.
623 MachineInstr *MI =
624 new MachineInstr(BB, TII->get(TargetInstrInfo::INSERT_SUBREG));
625
626 // Figure out the register class to create for the destreg.
627 const TargetRegisterClass *TRC = 0;
628 if (VRBase) {
629 TRC = RegMap->getRegClass(VRBase);
630 } else {
631 TRC = getSuperregRegisterClass(RegMap->getRegClass(SubReg),
632 SubIdx,
633 Node->getValueType(0));
634 assert(TRC && "Couldn't determine register class for insert_subreg");
635 VRBase = RegMap->createVirtualRegister(TRC); // Create the reg
636 }
637
Chris Lattner8019f412007-12-30 00:41:17 +0000638 MI->addOperand(MachineOperand::CreateReg(VRBase, true));
Christopher Lambe24f8f12007-07-26 08:12:07 +0000639 AddOperand(MI, Node->getOperand(0), 0, 0, VRBaseMap);
640 if (!isUndefInput)
641 AddOperand(MI, Node->getOperand(1), 0, 0, VRBaseMap);
Chris Lattnerfec65d52007-12-30 00:51:11 +0000642 MI->addOperand(MachineOperand::CreateImm(SubIdx));
Christopher Lambe24f8f12007-07-26 08:12:07 +0000643 } else
644 assert(0 && "Node is not a subreg insert or extract");
645
646 bool isNew = VRBaseMap.insert(std::make_pair(SDOperand(Node,0), VRBase));
647 assert(isNew && "Node emitted out of order - early");
648}
649
Jim Laskeyb6d4c2c2005-09-30 19:15:27 +0000650/// EmitNode - Generate machine code for an node and needed dependencies.
Jim Laskeye6b90fb2005-09-26 21:57:04 +0000651///
Evan Chenga6fb1b62007-09-25 01:54:36 +0000652void ScheduleDAG::EmitNode(SDNode *Node, unsigned InstanceNo,
Evan Chengaf825c82007-07-10 07:08:32 +0000653 DenseMap<SDOperand, unsigned> &VRBaseMap) {
Jim Laskeyb6d4c2c2005-09-30 19:15:27 +0000654 // If machine instruction
655 if (Node->isTargetOpcode()) {
656 unsigned Opc = Node->getTargetOpcode();
Christopher Lambe24f8f12007-07-26 08:12:07 +0000657
658 // Handle subreg insert/extract specially
659 if (Opc == TargetInstrInfo::EXTRACT_SUBREG ||
660 Opc == TargetInstrInfo::INSERT_SUBREG) {
661 EmitSubregNode(Node, VRBaseMap);
662 return;
663 }
664
Evan Chenga9c20912006-01-21 02:32:06 +0000665 const TargetInstrDescriptor &II = TII->get(Opc);
Chris Lattner2d973e42005-08-18 20:07:59 +0000666
Jim Laskeyb6d4c2c2005-09-30 19:15:27 +0000667 unsigned NumResults = CountResults(Node);
668 unsigned NodeOperands = CountOperands(Node);
Jim Laskeye6b90fb2005-09-26 21:57:04 +0000669 unsigned NumMIOperands = NodeOperands + NumResults;
Evan Cheng84097472007-08-02 00:28:15 +0000670 bool HasPhysRegOuts = (NumResults > II.numDefs) && II.ImplicitDefs;
Chris Lattnerda8abb02005-09-01 18:44:10 +0000671#ifndef NDEBUG
Evan Cheng8d3af5e2006-06-15 07:22:16 +0000672 assert((unsigned(II.numOperands) == NumMIOperands ||
Evan Cheng84097472007-08-02 00:28:15 +0000673 HasPhysRegOuts || (II.Flags & M_VARIABLE_OPS)) &&
Chris Lattner2d973e42005-08-18 20:07:59 +0000674 "#operands for dag node doesn't match .td file!");
Chris Lattnerca6aa2f2005-08-19 01:01:34 +0000675#endif
Chris Lattner2d973e42005-08-18 20:07:59 +0000676
677 // Create the new machine instruction.
Evan Chengc0f64ff2006-11-27 23:37:22 +0000678 MachineInstr *MI = new MachineInstr(II);
Chris Lattner2d973e42005-08-18 20:07:59 +0000679
680 // Add result register values for things that are defined by this
681 // instruction.
Evan Chengaf825c82007-07-10 07:08:32 +0000682 if (NumResults)
Evan Cheng84097472007-08-02 00:28:15 +0000683 CreateVirtualRegisters(Node, MI, II, VRBaseMap);
Jim Laskeyb6d4c2c2005-09-30 19:15:27 +0000684
685 // Emit all of the actual operands of this instruction, adding them to the
686 // instruction as appropriate.
Chris Lattnered18b682006-02-24 18:54:03 +0000687 for (unsigned i = 0; i != NodeOperands; ++i)
Evan Cheng84097472007-08-02 00:28:15 +0000688 AddOperand(MI, Node->getOperand(i), i+II.numDefs, &II, VRBaseMap);
Evan Cheng13d41b92006-05-12 01:58:24 +0000689
690 // Commute node if it has been determined to be profitable.
691 if (CommuteSet.count(Node)) {
692 MachineInstr *NewMI = TII->commuteInstruction(MI);
693 if (NewMI == 0)
Bill Wendling832171c2006-12-07 20:04:42 +0000694 DOUT << "Sched: COMMUTING FAILED!\n";
Evan Cheng13d41b92006-05-12 01:58:24 +0000695 else {
Bill Wendling832171c2006-12-07 20:04:42 +0000696 DOUT << "Sched: COMMUTED TO: " << *NewMI;
Evan Cheng4c6f2f92006-05-31 18:03:39 +0000697 if (MI != NewMI) {
698 delete MI;
699 MI = NewMI;
700 }
Evan Cheng13d41b92006-05-12 01:58:24 +0000701 }
702 }
703
Jim Laskeyb6d4c2c2005-09-30 19:15:27 +0000704 // Now that we have emitted all operands, emit this instruction itself.
705 if ((II.Flags & M_USES_CUSTOM_DAG_SCHED_INSERTION) == 0) {
706 BB->insert(BB->end(), MI);
707 } else {
708 // Insert this instruction into the end of the basic block, potentially
709 // taking some custom action.
710 BB = DAG.getTargetLoweringInfo().InsertAtEndOfBasicBlock(MI, BB);
711 }
Evan Cheng84097472007-08-02 00:28:15 +0000712
713 // Additional results must be an physical register def.
714 if (HasPhysRegOuts) {
715 for (unsigned i = II.numDefs; i < NumResults; ++i) {
716 unsigned Reg = II.ImplicitDefs[i - II.numDefs];
Evan Cheng33d55952007-08-02 05:29:38 +0000717 if (Node->hasAnyUseOfValue(i))
Evan Chenga6fb1b62007-09-25 01:54:36 +0000718 EmitCopyFromReg(Node, i, InstanceNo, Reg, VRBaseMap);
Evan Cheng84097472007-08-02 00:28:15 +0000719 }
720 }
Jim Laskeyb6d4c2c2005-09-30 19:15:27 +0000721 } else {
722 switch (Node->getOpcode()) {
723 default:
Jim Laskey16d42c62006-07-11 18:25:13 +0000724#ifndef NDEBUG
Dan Gohmanb5bec2b2007-06-19 14:13:56 +0000725 Node->dump(&DAG);
Jim Laskey16d42c62006-07-11 18:25:13 +0000726#endif
Jim Laskeyb6d4c2c2005-09-30 19:15:27 +0000727 assert(0 && "This target-independent node should have been selected!");
728 case ISD::EntryToken: // fall thru
729 case ISD::TokenFactor:
Jim Laskey1ee29252007-01-26 14:34:52 +0000730 case ISD::LABEL:
Jim Laskeyb6d4c2c2005-09-30 19:15:27 +0000731 break;
732 case ISD::CopyToReg: {
Evan Cheng489a87c2007-01-05 20:59:06 +0000733 unsigned InReg;
734 if (RegisterSDNode *R = dyn_cast<RegisterSDNode>(Node->getOperand(2)))
735 InReg = R->getReg();
736 else
737 InReg = getVR(Node->getOperand(2), VRBaseMap);
Chris Lattnera4176522005-10-30 18:54:27 +0000738 unsigned DestReg = cast<RegisterSDNode>(Node->getOperand(1))->getReg();
Lauro Ramos Venancio8334b9f2007-03-20 16:46:44 +0000739 if (InReg != DestReg) {// Coalesced away the copy?
740 const TargetRegisterClass *TRC = 0;
741 // Get the target register class
Lauro Ramos Venancioa0a26b72007-03-20 20:09:03 +0000742 if (MRegisterInfo::isVirtualRegister(InReg))
Lauro Ramos Venancio8334b9f2007-03-20 16:46:44 +0000743 TRC = RegMap->getRegClass(InReg);
Lauro Ramos Venancioa0a26b72007-03-20 20:09:03 +0000744 else
Evan Cheng42d60272007-09-26 21:36:17 +0000745 TRC =
746 MRI->getPhysicalRegisterRegClass(Node->getOperand(2).getValueType(),
Lauro Ramos Venancioa0a26b72007-03-20 20:09:03 +0000747 InReg);
Evan Cheng9efce632007-09-26 06:25:56 +0000748 MRI->copyRegToReg(*BB, BB->end(), DestReg, InReg, TRC, TRC);
Lauro Ramos Venancio8334b9f2007-03-20 16:46:44 +0000749 }
Jim Laskeyb6d4c2c2005-09-30 19:15:27 +0000750 break;
751 }
752 case ISD::CopyFromReg: {
753 unsigned SrcReg = cast<RegisterSDNode>(Node->getOperand(1))->getReg();
Evan Chenga6fb1b62007-09-25 01:54:36 +0000754 EmitCopyFromReg(Node, 0, InstanceNo, SrcReg, VRBaseMap);
Jim Laskeyb6d4c2c2005-09-30 19:15:27 +0000755 break;
756 }
Chris Lattneracc43bf2006-01-26 23:28:04 +0000757 case ISD::INLINEASM: {
758 unsigned NumOps = Node->getNumOperands();
759 if (Node->getOperand(NumOps-1).getValueType() == MVT::Flag)
760 --NumOps; // Ignore the flag operand.
761
762 // Create the inline asm machine instruction.
763 MachineInstr *MI =
Evan Chengc0f64ff2006-11-27 23:37:22 +0000764 new MachineInstr(BB, TII->get(TargetInstrInfo::INLINEASM));
Chris Lattneracc43bf2006-01-26 23:28:04 +0000765
766 // Add the asm string as an external symbol operand.
767 const char *AsmStr =
768 cast<ExternalSymbolSDNode>(Node->getOperand(1))->getSymbol();
Chris Lattnerfec65d52007-12-30 00:51:11 +0000769 MI->addOperand(MachineOperand::CreateES(AsmStr));
Chris Lattneracc43bf2006-01-26 23:28:04 +0000770
771 // Add all of the operand registers to the instruction.
Chris Lattnerc3a9f8d2006-02-23 19:21:04 +0000772 for (unsigned i = 2; i != NumOps;) {
773 unsigned Flags = cast<ConstantSDNode>(Node->getOperand(i))->getValue();
Chris Lattnerfd6d2822006-02-24 19:18:20 +0000774 unsigned NumVals = Flags >> 3;
Chris Lattnerc3a9f8d2006-02-23 19:21:04 +0000775
Chris Lattnerfec65d52007-12-30 00:51:11 +0000776 MI->addOperand(MachineOperand::CreateImm(Flags));
Chris Lattnerc3a9f8d2006-02-23 19:21:04 +0000777 ++i; // Skip the ID value.
778
779 switch (Flags & 7) {
Chris Lattneracc43bf2006-01-26 23:28:04 +0000780 default: assert(0 && "Bad flags!");
Chris Lattnerc3a9f8d2006-02-23 19:21:04 +0000781 case 1: // Use of register.
Chris Lattnerfd6d2822006-02-24 19:18:20 +0000782 for (; NumVals; --NumVals, ++i) {
Chris Lattnerc3a9f8d2006-02-23 19:21:04 +0000783 unsigned Reg = cast<RegisterSDNode>(Node->getOperand(i))->getReg();
Chris Lattner8019f412007-12-30 00:41:17 +0000784 MI->addOperand(MachineOperand::CreateReg(Reg, false));
Chris Lattnerc3a9f8d2006-02-23 19:21:04 +0000785 }
Chris Lattnerdc19b702006-02-04 02:26:14 +0000786 break;
Chris Lattnerc3a9f8d2006-02-23 19:21:04 +0000787 case 2: // Def 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 Lattner8019f412007-12-30 00:41:17 +0000790 MI->addOperand(MachineOperand::CreateReg(Reg, true));
Chris Lattnerc3a9f8d2006-02-23 19:21:04 +0000791 }
Chris Lattnerdc19b702006-02-04 02:26:14 +0000792 break;
Chris Lattnerdc19b702006-02-04 02:26:14 +0000793 case 3: { // Immediate.
Chris Lattner7df31dc2007-08-25 00:53:07 +0000794 for (; NumVals; --NumVals, ++i) {
795 if (ConstantSDNode *CS =
796 dyn_cast<ConstantSDNode>(Node->getOperand(i))) {
Chris Lattner8019f412007-12-30 00:41:17 +0000797 MI->addOperand(MachineOperand::CreateImm(CS->getValue()));
Dale Johanneseneb57ea72007-11-05 21:20:28 +0000798 } else if (GlobalAddressSDNode *GA =
799 dyn_cast<GlobalAddressSDNode>(Node->getOperand(i))) {
Chris Lattnerfec65d52007-12-30 00:51:11 +0000800 MI->addOperand(MachineOperand::CreateGA(GA->getGlobal(),
801 GA->getOffset()));
Dale Johanneseneb57ea72007-11-05 21:20:28 +0000802 } else {
Chris Lattnerfec65d52007-12-30 00:51:11 +0000803 BasicBlockSDNode *BB =cast<BasicBlockSDNode>(Node->getOperand(i));
804 MI->addOperand(MachineOperand::CreateMBB(BB->getBasicBlock()));
Chris Lattner7df31dc2007-08-25 00:53:07 +0000805 }
Chris Lattnerefa46ce2006-10-31 20:01:56 +0000806 }
Chris Lattnerdc19b702006-02-04 02:26:14 +0000807 break;
808 }
Chris Lattnerfd6d2822006-02-24 19:18:20 +0000809 case 4: // Addressing mode.
810 // The addressing mode has been selected, just add all of the
811 // operands to the machine instruction.
812 for (; NumVals; --NumVals, ++i)
Chris Lattnerdf375062006-03-10 07:25:12 +0000813 AddOperand(MI, Node->getOperand(i), 0, 0, VRBaseMap);
Chris Lattnerfd6d2822006-02-24 19:18:20 +0000814 break;
Chris Lattnerdc19b702006-02-04 02:26:14 +0000815 }
Chris Lattneracc43bf2006-01-26 23:28:04 +0000816 }
817 break;
818 }
Jim Laskeyb6d4c2c2005-09-30 19:15:27 +0000819 }
820 }
Jim Laskeyb6d4c2c2005-09-30 19:15:27 +0000821}
822
Chris Lattnera93dfcd2006-03-05 23:51:47 +0000823void ScheduleDAG::EmitNoop() {
824 TII->insertNoop(*BB, BB->end());
825}
826
Evan Cheng42d60272007-09-26 21:36:17 +0000827void ScheduleDAG::EmitCrossRCCopy(SUnit *SU, DenseMap<SUnit*, unsigned> &VRBaseMap) {
828 for (SUnit::const_pred_iterator I = SU->Preds.begin(), E = SU->Preds.end();
829 I != E; ++I) {
830 if (I->isCtrl) continue; // ignore chain preds
831 if (!I->Dep->Node) {
832 // Copy to physical register.
833 DenseMap<SUnit*, unsigned>::iterator VRI = VRBaseMap.find(I->Dep);
834 assert(VRI != VRBaseMap.end() && "Node emitted out of order - late");
835 // Find the destination physical register.
836 unsigned Reg = 0;
837 for (SUnit::const_succ_iterator II = SU->Succs.begin(),
838 EE = SU->Succs.end(); II != EE; ++II) {
839 if (I->Reg) {
840 Reg = I->Reg;
841 break;
842 }
843 }
844 assert(I->Reg && "Unknown physical register!");
845 MRI->copyRegToReg(*BB, BB->end(), Reg, VRI->second,
846 SU->CopyDstRC, SU->CopySrcRC);
847 } else {
848 // Copy from physical register.
849 assert(I->Reg && "Unknown physical register!");
850 unsigned VRBase = RegMap->createVirtualRegister(SU->CopyDstRC);
851 bool isNew = VRBaseMap.insert(std::make_pair(SU, VRBase));
852 assert(isNew && "Node emitted out of order - early");
853 MRI->copyRegToReg(*BB, BB->end(), VRBase, I->Reg,
854 SU->CopyDstRC, SU->CopySrcRC);
855 }
856 break;
857 }
858}
859
Evan Chenge165a782006-05-11 23:55:42 +0000860/// EmitSchedule - Emit the machine code in scheduled order.
861void ScheduleDAG::EmitSchedule() {
Chris Lattner96645412006-05-16 06:10:58 +0000862 // If this is the first basic block in the function, and if it has live ins
863 // that need to be copied into vregs, emit the copies into the top of the
864 // block before emitting the code for the block.
865 MachineFunction &MF = DAG.getMachineFunction();
Dan Gohmancb406c22007-10-03 19:26:29 +0000866 if (&MF.front() == BB) {
Chris Lattner96645412006-05-16 06:10:58 +0000867 for (MachineFunction::livein_iterator LI = MF.livein_begin(),
868 E = MF.livein_end(); LI != E; ++LI)
Evan Cheng9efce632007-09-26 06:25:56 +0000869 if (LI->second) {
870 const TargetRegisterClass *RC = RegMap->getRegClass(LI->second);
Chris Lattner96645412006-05-16 06:10:58 +0000871 MRI->copyRegToReg(*MF.begin(), MF.begin()->end(), LI->second,
Evan Cheng9efce632007-09-26 06:25:56 +0000872 LI->first, RC, RC);
873 }
Chris Lattner96645412006-05-16 06:10:58 +0000874 }
875
876
877 // Finally, emit the code for all of the scheduled instructions.
Evan Chengaf825c82007-07-10 07:08:32 +0000878 DenseMap<SDOperand, unsigned> VRBaseMap;
Evan Cheng42d60272007-09-26 21:36:17 +0000879 DenseMap<SUnit*, unsigned> CopyVRBaseMap;
Evan Chenge165a782006-05-11 23:55:42 +0000880 for (unsigned i = 0, e = Sequence.size(); i != e; i++) {
881 if (SUnit *SU = Sequence[i]) {
Evan Chenga6fb1b62007-09-25 01:54:36 +0000882 for (unsigned j = 0, ee = SU->FlaggedNodes.size(); j != ee; ++j)
883 EmitNode(SU->FlaggedNodes[j], SU->InstanceNo, VRBaseMap);
Evan Cheng42d60272007-09-26 21:36:17 +0000884 if (SU->Node)
885 EmitNode(SU->Node, SU->InstanceNo, VRBaseMap);
886 else
887 EmitCrossRCCopy(SU, CopyVRBaseMap);
Evan Chenge165a782006-05-11 23:55:42 +0000888 } else {
889 // Null SUnit* is a noop.
890 EmitNoop();
891 }
892 }
893}
894
895/// dump - dump the schedule.
896void ScheduleDAG::dumpSchedule() const {
897 for (unsigned i = 0, e = Sequence.size(); i != e; i++) {
898 if (SUnit *SU = Sequence[i])
899 SU->dump(&DAG);
900 else
Bill Wendling832171c2006-12-07 20:04:42 +0000901 cerr << "**** NOOP ****\n";
Evan Chenge165a782006-05-11 23:55:42 +0000902 }
903}
904
905
Evan Chenga9c20912006-01-21 02:32:06 +0000906/// Run - perform scheduling.
907///
908MachineBasicBlock *ScheduleDAG::Run() {
909 TII = TM.getInstrInfo();
910 MRI = TM.getRegisterInfo();
911 RegMap = BB->getParent()->getSSARegMap();
912 ConstPool = BB->getParent()->getConstantPool();
Evan Cheng4ef10862006-01-23 07:01:07 +0000913
Evan Chenga9c20912006-01-21 02:32:06 +0000914 Schedule();
915 return BB;
Chris Lattnerd32b2362005-08-18 18:45:24 +0000916}
Evan Cheng4ef10862006-01-23 07:01:07 +0000917
Evan Chenge165a782006-05-11 23:55:42 +0000918/// SUnit - Scheduling unit. It's an wrapper around either a single SDNode or
919/// a group of nodes flagged together.
920void SUnit::dump(const SelectionDAG *G) const {
Bill Wendling832171c2006-12-07 20:04:42 +0000921 cerr << "SU(" << NodeNum << "): ";
Evan Cheng42d60272007-09-26 21:36:17 +0000922 if (Node)
923 Node->dump(G);
924 else
925 cerr << "CROSS RC COPY ";
Bill Wendling832171c2006-12-07 20:04:42 +0000926 cerr << "\n";
Evan Chenge165a782006-05-11 23:55:42 +0000927 if (FlaggedNodes.size() != 0) {
928 for (unsigned i = 0, e = FlaggedNodes.size(); i != e; i++) {
Bill Wendling832171c2006-12-07 20:04:42 +0000929 cerr << " ";
Evan Chenge165a782006-05-11 23:55:42 +0000930 FlaggedNodes[i]->dump(G);
Bill Wendling832171c2006-12-07 20:04:42 +0000931 cerr << "\n";
Evan Chenge165a782006-05-11 23:55:42 +0000932 }
933 }
934}
Evan Cheng4ef10862006-01-23 07:01:07 +0000935
Evan Chenge165a782006-05-11 23:55:42 +0000936void SUnit::dumpAll(const SelectionDAG *G) const {
937 dump(G);
938
Bill Wendling832171c2006-12-07 20:04:42 +0000939 cerr << " # preds left : " << NumPredsLeft << "\n";
940 cerr << " # succs left : " << NumSuccsLeft << "\n";
Bill Wendling832171c2006-12-07 20:04:42 +0000941 cerr << " Latency : " << Latency << "\n";
942 cerr << " Depth : " << Depth << "\n";
943 cerr << " Height : " << Height << "\n";
Evan Chenge165a782006-05-11 23:55:42 +0000944
945 if (Preds.size() != 0) {
Bill Wendling832171c2006-12-07 20:04:42 +0000946 cerr << " Predecessors:\n";
Chris Lattner228a18e2006-08-17 00:09:56 +0000947 for (SUnit::const_succ_iterator I = Preds.begin(), E = Preds.end();
948 I != E; ++I) {
Evan Cheng713a98d2007-09-19 01:38:40 +0000949 if (I->isCtrl)
Bill Wendling832171c2006-12-07 20:04:42 +0000950 cerr << " ch #";
Evan Chenge165a782006-05-11 23:55:42 +0000951 else
Bill Wendling832171c2006-12-07 20:04:42 +0000952 cerr << " val #";
Evan Chenga6fb1b62007-09-25 01:54:36 +0000953 cerr << I->Dep << " - SU(" << I->Dep->NodeNum << ")";
954 if (I->isSpecial)
955 cerr << " *";
956 cerr << "\n";
Evan Chenge165a782006-05-11 23:55:42 +0000957 }
958 }
959 if (Succs.size() != 0) {
Bill Wendling832171c2006-12-07 20:04:42 +0000960 cerr << " Successors:\n";
Chris Lattner228a18e2006-08-17 00:09:56 +0000961 for (SUnit::const_succ_iterator I = Succs.begin(), E = Succs.end();
962 I != E; ++I) {
Evan Cheng713a98d2007-09-19 01:38:40 +0000963 if (I->isCtrl)
Bill Wendling832171c2006-12-07 20:04:42 +0000964 cerr << " ch #";
Evan Chenge165a782006-05-11 23:55:42 +0000965 else
Bill Wendling832171c2006-12-07 20:04:42 +0000966 cerr << " val #";
Evan Chenga6fb1b62007-09-25 01:54:36 +0000967 cerr << I->Dep << " - SU(" << I->Dep->NodeNum << ")";
968 if (I->isSpecial)
969 cerr << " *";
970 cerr << "\n";
Evan Chenge165a782006-05-11 23:55:42 +0000971 }
972 }
Bill Wendling832171c2006-12-07 20:04:42 +0000973 cerr << "\n";
Evan Chenge165a782006-05-11 23:55:42 +0000974}