blob: da79a991c243421bf0e59d0d4c3ba6cf7b7aab0e [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"
Nate Begemane1795842008-02-14 08:57:00 +000017#include "llvm/Constants.h"
Reid Spencere5530da2007-01-12 23:31:12 +000018#include "llvm/Type.h"
Chris Lattnerb0d21ef2006-03-08 04:25:59 +000019#include "llvm/CodeGen/ScheduleDAG.h"
Chris Lattner5839bf22005-08-26 17:15:30 +000020#include "llvm/CodeGen/MachineConstantPool.h"
Chris Lattner4ccd4062005-08-19 20:45:43 +000021#include "llvm/CodeGen/MachineFunction.h"
Chris Lattner84bc5422007-12-31 04:13:23 +000022#include "llvm/CodeGen/MachineRegisterInfo.h"
Owen Anderson07000c62006-05-12 06:33:49 +000023#include "llvm/Target/TargetData.h"
Chris Lattner2d973e42005-08-18 20:07:59 +000024#include "llvm/Target/TargetMachine.h"
25#include "llvm/Target/TargetInstrInfo.h"
Chris Lattner025c39b2005-08-26 20:54:47 +000026#include "llvm/Target/TargetLowering.h"
Evan Chenge165a782006-05-11 23:55:42 +000027#include "llvm/Support/Debug.h"
Chris Lattner54a30b92006-03-20 01:51:46 +000028#include "llvm/Support/MathExtras.h"
Chris Lattnerd32b2362005-08-18 18:45:24 +000029using namespace llvm;
30
Chris Lattner84bc5422007-12-31 04:13:23 +000031ScheduleDAG::ScheduleDAG(SelectionDAG &dag, MachineBasicBlock *bb,
32 const TargetMachine &tm)
33 : DAG(dag), BB(bb), TM(tm), RegInfo(BB->getParent()->getRegInfo()) {
34 TII = TM.getInstrInfo();
Evan Cheng6b2cf282008-01-30 19:35:32 +000035 MF = &DAG.getMachineFunction();
Dan Gohman6f0d0242008-02-10 18:45:23 +000036 TRI = TM.getRegisterInfo();
Chris Lattner84bc5422007-12-31 04:13:23 +000037 ConstPool = BB->getParent()->getConstantPool();
38}
Evan Chenga6fb1b62007-09-25 01:54:36 +000039
Evan Chenga6fb1b62007-09-25 01:54:36 +000040/// CheckForPhysRegDependency - Check if the dependency between def and use of
41/// a specified operand is a physical register dependency. If so, returns the
42/// register and the cost of copying the register.
43static void CheckForPhysRegDependency(SDNode *Def, SDNode *Use, unsigned Op,
Dan Gohman6f0d0242008-02-10 18:45:23 +000044 const TargetRegisterInfo *TRI,
Evan Chenga6fb1b62007-09-25 01:54:36 +000045 const TargetInstrInfo *TII,
46 unsigned &PhysReg, int &Cost) {
47 if (Op != 2 || Use->getOpcode() != ISD::CopyToReg)
48 return;
49
50 unsigned Reg = cast<RegisterSDNode>(Use->getOperand(1))->getReg();
Dan Gohman6f0d0242008-02-10 18:45:23 +000051 if (TargetRegisterInfo::isVirtualRegister(Reg))
Evan Chenga6fb1b62007-09-25 01:54:36 +000052 return;
53
54 unsigned ResNo = Use->getOperand(2).ResNo;
55 if (Def->isTargetOpcode()) {
Chris Lattner749c6f62008-01-07 07:27:27 +000056 const TargetInstrDesc &II = TII->get(Def->getTargetOpcode());
Chris Lattner349c4952008-01-07 03:13:06 +000057 if (ResNo >= II.getNumDefs() &&
58 II.ImplicitDefs[ResNo - II.getNumDefs()] == Reg) {
Evan Chenga6fb1b62007-09-25 01:54:36 +000059 PhysReg = Reg;
60 const TargetRegisterClass *RC =
Dan Gohman6f0d0242008-02-10 18:45:23 +000061 TRI->getPhysicalRegisterRegClass(Def->getValueType(ResNo), Reg);
Evan Chenga6fb1b62007-09-25 01:54:36 +000062 Cost = RC->getCopyCost();
63 }
64 }
65}
66
67SUnit *ScheduleDAG::Clone(SUnit *Old) {
68 SUnit *SU = NewSUnit(Old->Node);
69 for (unsigned i = 0, e = SU->FlaggedNodes.size(); i != e; ++i)
70 SU->FlaggedNodes.push_back(SU->FlaggedNodes[i]);
71 SU->InstanceNo = SUnitMap[Old->Node].size();
72 SU->Latency = Old->Latency;
73 SU->isTwoAddress = Old->isTwoAddress;
74 SU->isCommutable = Old->isCommutable;
Evan Cheng22a52992007-09-28 22:32:30 +000075 SU->hasPhysRegDefs = Old->hasPhysRegDefs;
Evan Chenga6fb1b62007-09-25 01:54:36 +000076 SUnitMap[Old->Node].push_back(SU);
77 return SU;
78}
79
Evan Chengf10c9732007-10-05 01:39:18 +000080
Evan Chenge165a782006-05-11 23:55:42 +000081/// BuildSchedUnits - Build SUnits from the selection dag that we are input.
82/// This SUnit graph is similar to the SelectionDAG, but represents flagged
83/// together nodes with a single SUnit.
84void ScheduleDAG::BuildSchedUnits() {
85 // Reserve entries in the vector for each of the SUnits we are creating. This
86 // ensure that reallocation of the vector won't happen, so SUnit*'s won't get
87 // invalidated.
88 SUnits.reserve(std::distance(DAG.allnodes_begin(), DAG.allnodes_end()));
89
Evan Chenge165a782006-05-11 23:55:42 +000090 for (SelectionDAG::allnodes_iterator NI = DAG.allnodes_begin(),
91 E = DAG.allnodes_end(); NI != E; ++NI) {
92 if (isPassiveNode(NI)) // Leaf node, e.g. a TargetImmediate.
93 continue;
94
95 // If this node has already been processed, stop now.
Evan Chenga6fb1b62007-09-25 01:54:36 +000096 if (SUnitMap[NI].size()) continue;
Evan Chenge165a782006-05-11 23:55:42 +000097
98 SUnit *NodeSUnit = NewSUnit(NI);
99
100 // See if anything is flagged to this node, if so, add them to flagged
101 // nodes. Nodes can have at most one flag input and one flag output. Flags
102 // are required the be the last operand and result of a node.
103
104 // Scan up, adding flagged preds to FlaggedNodes.
105 SDNode *N = NI;
Evan Cheng3b97acd2006-08-07 22:12:12 +0000106 if (N->getNumOperands() &&
107 N->getOperand(N->getNumOperands()-1).getValueType() == MVT::Flag) {
108 do {
109 N = N->getOperand(N->getNumOperands()-1).Val;
110 NodeSUnit->FlaggedNodes.push_back(N);
Evan Chenga6fb1b62007-09-25 01:54:36 +0000111 SUnitMap[N].push_back(NodeSUnit);
Evan Cheng3b97acd2006-08-07 22:12:12 +0000112 } while (N->getNumOperands() &&
113 N->getOperand(N->getNumOperands()-1).getValueType()== MVT::Flag);
114 std::reverse(NodeSUnit->FlaggedNodes.begin(),
115 NodeSUnit->FlaggedNodes.end());
Evan Chenge165a782006-05-11 23:55:42 +0000116 }
117
118 // Scan down, adding this node and any flagged succs to FlaggedNodes if they
119 // have a user of the flag operand.
120 N = NI;
121 while (N->getValueType(N->getNumValues()-1) == MVT::Flag) {
122 SDOperand FlagVal(N, N->getNumValues()-1);
123
124 // There are either zero or one users of the Flag result.
125 bool HasFlagUse = false;
126 for (SDNode::use_iterator UI = N->use_begin(), E = N->use_end();
127 UI != E; ++UI)
128 if (FlagVal.isOperand(*UI)) {
129 HasFlagUse = true;
130 NodeSUnit->FlaggedNodes.push_back(N);
Evan Chenga6fb1b62007-09-25 01:54:36 +0000131 SUnitMap[N].push_back(NodeSUnit);
Evan Chenge165a782006-05-11 23:55:42 +0000132 N = *UI;
133 break;
134 }
Chris Lattner228a18e2006-08-17 00:09:56 +0000135 if (!HasFlagUse) break;
Evan Chenge165a782006-05-11 23:55:42 +0000136 }
137
138 // Now all flagged nodes are in FlaggedNodes and N is the bottom-most node.
139 // Update the SUnit
140 NodeSUnit->Node = N;
Evan Chenga6fb1b62007-09-25 01:54:36 +0000141 SUnitMap[N].push_back(NodeSUnit);
Evan Chengf10c9732007-10-05 01:39:18 +0000142
143 ComputeLatency(NodeSUnit);
Evan Chenge165a782006-05-11 23:55:42 +0000144 }
145
146 // Pass 2: add the preds, succs, etc.
147 for (unsigned su = 0, e = SUnits.size(); su != e; ++su) {
148 SUnit *SU = &SUnits[su];
149 SDNode *MainNode = SU->Node;
150
151 if (MainNode->isTargetOpcode()) {
152 unsigned Opc = MainNode->getTargetOpcode();
Chris Lattner749c6f62008-01-07 07:27:27 +0000153 const TargetInstrDesc &TID = TII->get(Opc);
Chris Lattner349c4952008-01-07 03:13:06 +0000154 for (unsigned i = 0; i != TID.getNumOperands(); ++i) {
Evan Chenga6fb1b62007-09-25 01:54:36 +0000155 if (TID.getOperandConstraint(i, TOI::TIED_TO) != -1) {
Evan Cheng95f6ede2006-11-04 09:44:31 +0000156 SU->isTwoAddress = true;
157 break;
158 }
159 }
Chris Lattner0ff23962008-01-07 06:42:05 +0000160 if (TID.isCommutable())
Evan Cheng13d41b92006-05-12 01:58:24 +0000161 SU->isCommutable = true;
Evan Chenge165a782006-05-11 23:55:42 +0000162 }
163
164 // Find all predecessors and successors of the group.
165 // Temporarily add N to make code simpler.
166 SU->FlaggedNodes.push_back(MainNode);
167
168 for (unsigned n = 0, e = SU->FlaggedNodes.size(); n != e; ++n) {
169 SDNode *N = SU->FlaggedNodes[n];
Evan Cheng22a52992007-09-28 22:32:30 +0000170 if (N->isTargetOpcode() &&
Chris Lattner349c4952008-01-07 03:13:06 +0000171 TII->get(N->getTargetOpcode()).getImplicitDefs() &&
172 CountResults(N) > TII->get(N->getTargetOpcode()).getNumDefs())
Evan Cheng22a52992007-09-28 22:32:30 +0000173 SU->hasPhysRegDefs = true;
Evan Chenge165a782006-05-11 23:55:42 +0000174
175 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
176 SDNode *OpN = N->getOperand(i).Val;
177 if (isPassiveNode(OpN)) continue; // Not scheduled.
Evan Chenga6fb1b62007-09-25 01:54:36 +0000178 SUnit *OpSU = SUnitMap[OpN].front();
Evan Chenge165a782006-05-11 23:55:42 +0000179 assert(OpSU && "Node has no SUnit!");
180 if (OpSU == SU) continue; // In the same group.
181
182 MVT::ValueType OpVT = N->getOperand(i).getValueType();
183 assert(OpVT != MVT::Flag && "Flagged nodes should be in same sunit!");
184 bool isChain = OpVT == MVT::Other;
Evan Chenga6fb1b62007-09-25 01:54:36 +0000185
186 unsigned PhysReg = 0;
187 int Cost = 1;
188 // Determine if this is a physical register dependency.
Dan Gohman6f0d0242008-02-10 18:45:23 +0000189 CheckForPhysRegDependency(OpN, N, i, TRI, TII, PhysReg, Cost);
Evan Chenga6fb1b62007-09-25 01:54:36 +0000190 SU->addPred(OpSU, isChain, false, PhysReg, Cost);
Evan Chenge165a782006-05-11 23:55:42 +0000191 }
192 }
193
194 // Remove MainNode from FlaggedNodes again.
195 SU->FlaggedNodes.pop_back();
196 }
197
198 return;
199}
200
Evan Chengf10c9732007-10-05 01:39:18 +0000201void ScheduleDAG::ComputeLatency(SUnit *SU) {
202 const InstrItineraryData &InstrItins = TM.getInstrItineraryData();
203
204 // Compute the latency for the node. We use the sum of the latencies for
205 // all nodes flagged together into this SUnit.
206 if (InstrItins.isEmpty()) {
207 // No latency information.
208 SU->Latency = 1;
209 } else {
210 SU->Latency = 0;
211 if (SU->Node->isTargetOpcode()) {
Chris Lattnerba6da5d2008-01-07 02:46:03 +0000212 unsigned SchedClass =
213 TII->get(SU->Node->getTargetOpcode()).getSchedClass();
Evan Chengf10c9732007-10-05 01:39:18 +0000214 InstrStage *S = InstrItins.begin(SchedClass);
215 InstrStage *E = InstrItins.end(SchedClass);
216 for (; S != E; ++S)
217 SU->Latency += S->Cycles;
218 }
219 for (unsigned i = 0, e = SU->FlaggedNodes.size(); i != e; ++i) {
220 SDNode *FNode = SU->FlaggedNodes[i];
221 if (FNode->isTargetOpcode()) {
Chris Lattnerba6da5d2008-01-07 02:46:03 +0000222 unsigned SchedClass =TII->get(FNode->getTargetOpcode()).getSchedClass();
Evan Chengf10c9732007-10-05 01:39:18 +0000223 InstrStage *S = InstrItins.begin(SchedClass);
224 InstrStage *E = InstrItins.end(SchedClass);
225 for (; S != E; ++S)
226 SU->Latency += S->Cycles;
227 }
228 }
229 }
230}
231
Evan Chenge165a782006-05-11 23:55:42 +0000232void ScheduleDAG::CalculateDepths() {
Evan Cheng99126282007-07-06 01:37:28 +0000233 std::vector<std::pair<SUnit*, unsigned> > WorkList;
Evan Chenge165a782006-05-11 23:55:42 +0000234 for (unsigned i = 0, e = SUnits.size(); i != e; ++i)
Dan Gohman30359592008-01-29 13:02:09 +0000235 if (SUnits[i].Preds.empty())
Evan Cheng99126282007-07-06 01:37:28 +0000236 WorkList.push_back(std::make_pair(&SUnits[i], 0U));
Evan Chenge165a782006-05-11 23:55:42 +0000237
Evan Cheng99126282007-07-06 01:37:28 +0000238 while (!WorkList.empty()) {
239 SUnit *SU = WorkList.back().first;
240 unsigned Depth = WorkList.back().second;
241 WorkList.pop_back();
242 if (SU->Depth == 0 || Depth > SU->Depth) {
243 SU->Depth = Depth;
244 for (SUnit::succ_iterator I = SU->Succs.begin(), E = SU->Succs.end();
245 I != E; ++I)
Evan Cheng713a98d2007-09-19 01:38:40 +0000246 WorkList.push_back(std::make_pair(I->Dep, Depth+1));
Evan Cheng99126282007-07-06 01:37:28 +0000247 }
Evan Cheng626da3d2006-05-12 06:05:18 +0000248 }
Evan Chenge165a782006-05-11 23:55:42 +0000249}
Evan Cheng99126282007-07-06 01:37:28 +0000250
Evan Chenge165a782006-05-11 23:55:42 +0000251void ScheduleDAG::CalculateHeights() {
Evan Cheng99126282007-07-06 01:37:28 +0000252 std::vector<std::pair<SUnit*, unsigned> > WorkList;
Evan Chenga6fb1b62007-09-25 01:54:36 +0000253 SUnit *Root = SUnitMap[DAG.getRoot().Val].front();
Evan Cheng99126282007-07-06 01:37:28 +0000254 WorkList.push_back(std::make_pair(Root, 0U));
255
256 while (!WorkList.empty()) {
257 SUnit *SU = WorkList.back().first;
258 unsigned Height = WorkList.back().second;
259 WorkList.pop_back();
260 if (SU->Height == 0 || Height > SU->Height) {
261 SU->Height = Height;
262 for (SUnit::pred_iterator I = SU->Preds.begin(), E = SU->Preds.end();
263 I != E; ++I)
Evan Cheng713a98d2007-09-19 01:38:40 +0000264 WorkList.push_back(std::make_pair(I->Dep, Height+1));
Evan Cheng99126282007-07-06 01:37:28 +0000265 }
266 }
Evan Chenge165a782006-05-11 23:55:42 +0000267}
268
Jim Laskeye6b90fb2005-09-26 21:57:04 +0000269/// CountResults - The results of target nodes have register or immediate
270/// operands first, then an optional chain, and optional flag operands (which do
Dan Gohman027ee7e2008-02-11 19:00:03 +0000271/// not go into the resulting MachineInstr).
Evan Cheng95f6ede2006-11-04 09:44:31 +0000272unsigned ScheduleDAG::CountResults(SDNode *Node) {
Jim Laskeyb6d4c2c2005-09-30 19:15:27 +0000273 unsigned N = Node->getNumValues();
274 while (N && Node->getValueType(N - 1) == MVT::Flag)
Jim Laskeye6b90fb2005-09-26 21:57:04 +0000275 --N;
Jim Laskeyb6d4c2c2005-09-30 19:15:27 +0000276 if (N && Node->getValueType(N - 1) == MVT::Other)
Jim Laskeye6b90fb2005-09-26 21:57:04 +0000277 --N; // Skip over chain result.
278 return N;
279}
280
Dan Gohman69de1932008-02-06 22:27:42 +0000281/// CountOperands - The inputs to target nodes have any actual inputs first,
Dan Gohman42a77882008-02-16 00:36:48 +0000282/// followed by special operands that describe memory references, then an
283/// optional chain operand, then flag operands. Compute the number of
284/// actual operands that will go into the resulting MachineInstr.
Evan Cheng95f6ede2006-11-04 09:44:31 +0000285unsigned ScheduleDAG::CountOperands(SDNode *Node) {
Dan Gohman42a77882008-02-16 00:36:48 +0000286 unsigned N = ComputeMemOperandsEnd(Node);
Dan Gohmancc20cd52008-02-11 19:00:34 +0000287 while (N && isa<MemOperandSDNode>(Node->getOperand(N - 1).Val))
Dan Gohman69de1932008-02-06 22:27:42 +0000288 --N; // Ignore MemOperand nodes
289 return N;
290}
291
Dan Gohman42a77882008-02-16 00:36:48 +0000292/// ComputeMemOperandsEnd - Find the index one past the last MemOperandSDNode
293/// operand
294unsigned ScheduleDAG::ComputeMemOperandsEnd(SDNode *Node) {
Dan Gohman69de1932008-02-06 22:27:42 +0000295 unsigned N = Node->getNumOperands();
296 while (N && Node->getOperand(N - 1).getValueType() == MVT::Flag)
297 --N;
298 if (N && Node->getOperand(N - 1).getValueType() == MVT::Other)
299 --N; // Ignore chain if it exists.
Jim Laskeye6b90fb2005-09-26 21:57:04 +0000300 return N;
301}
302
Jim Laskey60f09922006-07-21 20:57:35 +0000303static const TargetRegisterClass *getInstrOperandRegClass(
Dan Gohman6f0d0242008-02-10 18:45:23 +0000304 const TargetRegisterInfo *TRI,
Jim Laskey60f09922006-07-21 20:57:35 +0000305 const TargetInstrInfo *TII,
Chris Lattner749c6f62008-01-07 07:27:27 +0000306 const TargetInstrDesc &II,
Jim Laskey60f09922006-07-21 20:57:35 +0000307 unsigned Op) {
Chris Lattner749c6f62008-01-07 07:27:27 +0000308 if (Op >= II.getNumOperands()) {
309 assert(II.isVariadic() && "Invalid operand # of instruction");
Jim Laskey60f09922006-07-21 20:57:35 +0000310 return NULL;
311 }
Chris Lattner749c6f62008-01-07 07:27:27 +0000312 if (II.OpInfo[Op].isLookupPtrRegClass())
Chris Lattner8ca5c672008-01-07 02:39:19 +0000313 return TII->getPointerRegClass();
Dan Gohman6f0d0242008-02-10 18:45:23 +0000314 return TRI->getRegClass(II.OpInfo[Op].RegClass);
Jim Laskey60f09922006-07-21 20:57:35 +0000315}
316
Evan Chenga6fb1b62007-09-25 01:54:36 +0000317void ScheduleDAG::EmitCopyFromReg(SDNode *Node, unsigned ResNo,
318 unsigned InstanceNo, unsigned SrcReg,
Evan Cheng84097472007-08-02 00:28:15 +0000319 DenseMap<SDOperand, unsigned> &VRBaseMap) {
320 unsigned VRBase = 0;
Dan Gohman6f0d0242008-02-10 18:45:23 +0000321 if (TargetRegisterInfo::isVirtualRegister(SrcReg)) {
Evan Cheng84097472007-08-02 00:28:15 +0000322 // Just use the input register directly!
Evan Chenga6fb1b62007-09-25 01:54:36 +0000323 if (InstanceNo > 0)
324 VRBaseMap.erase(SDOperand(Node, ResNo));
Evan Cheng84097472007-08-02 00:28:15 +0000325 bool isNew = VRBaseMap.insert(std::make_pair(SDOperand(Node,ResNo),SrcReg));
326 assert(isNew && "Node emitted out of order - early");
327 return;
328 }
329
330 // If the node is only used by a CopyToReg and the dest reg is a vreg, use
331 // the CopyToReg'd destination register instead of creating a new vreg.
Evan Chenga6fb1b62007-09-25 01:54:36 +0000332 bool MatchReg = true;
Evan Cheng84097472007-08-02 00:28:15 +0000333 for (SDNode::use_iterator UI = Node->use_begin(), E = Node->use_end();
334 UI != E; ++UI) {
335 SDNode *Use = *UI;
Evan Chenga6fb1b62007-09-25 01:54:36 +0000336 bool Match = true;
Evan Cheng84097472007-08-02 00:28:15 +0000337 if (Use->getOpcode() == ISD::CopyToReg &&
338 Use->getOperand(2).Val == Node &&
339 Use->getOperand(2).ResNo == ResNo) {
340 unsigned DestReg = cast<RegisterSDNode>(Use->getOperand(1))->getReg();
Dan Gohman6f0d0242008-02-10 18:45:23 +0000341 if (TargetRegisterInfo::isVirtualRegister(DestReg)) {
Evan Cheng84097472007-08-02 00:28:15 +0000342 VRBase = DestReg;
Evan Chenga6fb1b62007-09-25 01:54:36 +0000343 Match = false;
344 } else if (DestReg != SrcReg)
345 Match = false;
346 } else {
347 for (unsigned i = 0, e = Use->getNumOperands(); i != e; ++i) {
348 SDOperand Op = Use->getOperand(i);
Evan Cheng7c07aeb2007-12-14 08:25:15 +0000349 if (Op.Val != Node || Op.ResNo != ResNo)
Evan Chenga6fb1b62007-09-25 01:54:36 +0000350 continue;
351 MVT::ValueType VT = Node->getValueType(Op.ResNo);
352 if (VT != MVT::Other && VT != MVT::Flag)
353 Match = false;
Evan Cheng84097472007-08-02 00:28:15 +0000354 }
355 }
Evan Chenga6fb1b62007-09-25 01:54:36 +0000356 MatchReg &= Match;
357 if (VRBase)
358 break;
Evan Cheng84097472007-08-02 00:28:15 +0000359 }
360
Evan Cheng84097472007-08-02 00:28:15 +0000361 const TargetRegisterClass *TRC = 0;
Evan Chenga6fb1b62007-09-25 01:54:36 +0000362 // Figure out the register class to create for the destreg.
363 if (VRBase)
Chris Lattner84bc5422007-12-31 04:13:23 +0000364 TRC = RegInfo.getRegClass(VRBase);
Evan Chenga6fb1b62007-09-25 01:54:36 +0000365 else
Dan Gohman6f0d0242008-02-10 18:45:23 +0000366 TRC = TRI->getPhysicalRegisterRegClass(Node->getValueType(ResNo), SrcReg);
Evan Chenga6fb1b62007-09-25 01:54:36 +0000367
368 // If all uses are reading from the src physical register and copying the
369 // register is either impossible or very expensive, then don't create a copy.
370 if (MatchReg && TRC->getCopyCost() < 0) {
371 VRBase = SrcReg;
372 } else {
Evan Cheng84097472007-08-02 00:28:15 +0000373 // Create the reg, emit the copy.
Chris Lattner84bc5422007-12-31 04:13:23 +0000374 VRBase = RegInfo.createVirtualRegister(TRC);
Owen Andersond10fd972007-12-31 06:32:00 +0000375 TII->copyRegToReg(*BB, BB->end(), VRBase, SrcReg, TRC, TRC);
Evan Cheng84097472007-08-02 00:28:15 +0000376 }
Evan Cheng84097472007-08-02 00:28:15 +0000377
Evan Chenga6fb1b62007-09-25 01:54:36 +0000378 if (InstanceNo > 0)
379 VRBaseMap.erase(SDOperand(Node, ResNo));
Evan Cheng84097472007-08-02 00:28:15 +0000380 bool isNew = VRBaseMap.insert(std::make_pair(SDOperand(Node,ResNo), VRBase));
381 assert(isNew && "Node emitted out of order - early");
382}
383
384void ScheduleDAG::CreateVirtualRegisters(SDNode *Node,
385 MachineInstr *MI,
Chris Lattner749c6f62008-01-07 07:27:27 +0000386 const TargetInstrDesc &II,
Evan Cheng84097472007-08-02 00:28:15 +0000387 DenseMap<SDOperand, unsigned> &VRBaseMap) {
Chris Lattner349c4952008-01-07 03:13:06 +0000388 for (unsigned i = 0; i < II.getNumDefs(); ++i) {
Evan Chengaf825c82007-07-10 07:08:32 +0000389 // If the specific node value is only used by a CopyToReg and the dest reg
390 // is a vreg, use the CopyToReg'd destination register instead of creating
391 // a new vreg.
392 unsigned VRBase = 0;
393 for (SDNode::use_iterator UI = Node->use_begin(), E = Node->use_end();
394 UI != E; ++UI) {
395 SDNode *Use = *UI;
396 if (Use->getOpcode() == ISD::CopyToReg &&
397 Use->getOperand(2).Val == Node &&
398 Use->getOperand(2).ResNo == i) {
399 unsigned Reg = cast<RegisterSDNode>(Use->getOperand(1))->getReg();
Dan Gohman6f0d0242008-02-10 18:45:23 +0000400 if (TargetRegisterInfo::isVirtualRegister(Reg)) {
Evan Chengaf825c82007-07-10 07:08:32 +0000401 VRBase = Reg;
Chris Lattner8019f412007-12-30 00:41:17 +0000402 MI->addOperand(MachineOperand::CreateReg(Reg, true));
Evan Chengaf825c82007-07-10 07:08:32 +0000403 break;
404 }
405 }
406 }
407
Evan Cheng84097472007-08-02 00:28:15 +0000408 // Create the result registers for this node and add the result regs to
409 // the machine instruction.
Evan Chengaf825c82007-07-10 07:08:32 +0000410 if (VRBase == 0) {
Dan Gohman6f0d0242008-02-10 18:45:23 +0000411 const TargetRegisterClass *RC = getInstrOperandRegClass(TRI, TII, II, i);
Evan Chengaf825c82007-07-10 07:08:32 +0000412 assert(RC && "Isn't a register operand!");
Chris Lattner84bc5422007-12-31 04:13:23 +0000413 VRBase = RegInfo.createVirtualRegister(RC);
Chris Lattner8019f412007-12-30 00:41:17 +0000414 MI->addOperand(MachineOperand::CreateReg(VRBase, true));
Evan Chengaf825c82007-07-10 07:08:32 +0000415 }
416
417 bool isNew = VRBaseMap.insert(std::make_pair(SDOperand(Node,i), VRBase));
418 assert(isNew && "Node emitted out of order - early");
Jim Laskeye6b90fb2005-09-26 21:57:04 +0000419 }
Jim Laskeye6b90fb2005-09-26 21:57:04 +0000420}
421
Chris Lattnerdf375062006-03-10 07:25:12 +0000422/// getVR - Return the virtual register corresponding to the specified result
423/// of the specified node.
Evan Chengaf825c82007-07-10 07:08:32 +0000424static unsigned getVR(SDOperand Op, DenseMap<SDOperand, unsigned> &VRBaseMap) {
425 DenseMap<SDOperand, unsigned>::iterator I = VRBaseMap.find(Op);
Chris Lattnerdf375062006-03-10 07:25:12 +0000426 assert(I != VRBaseMap.end() && "Node emitted out of order - late");
Evan Chengaf825c82007-07-10 07:08:32 +0000427 return I->second;
Chris Lattnerdf375062006-03-10 07:25:12 +0000428}
429
430
Chris Lattnered18b682006-02-24 18:54:03 +0000431/// AddOperand - Add the specified operand to the specified machine instr. II
432/// specifies the instruction information for the node, and IIOpNum is the
433/// operand number (in the II) that we are adding. IIOpNum and II are used for
434/// assertions only.
435void ScheduleDAG::AddOperand(MachineInstr *MI, SDOperand Op,
436 unsigned IIOpNum,
Chris Lattner749c6f62008-01-07 07:27:27 +0000437 const TargetInstrDesc *II,
Evan Chengaf825c82007-07-10 07:08:32 +0000438 DenseMap<SDOperand, unsigned> &VRBaseMap) {
Chris Lattnered18b682006-02-24 18:54:03 +0000439 if (Op.isTargetOpcode()) {
440 // Note that this case is redundant with the final else block, but we
441 // include it because it is the most common and it makes the logic
442 // simpler here.
443 assert(Op.getValueType() != MVT::Other &&
444 Op.getValueType() != MVT::Flag &&
445 "Chain and flag operands should occur at end of operand list!");
446
447 // Get/emit the operand.
Chris Lattnerdf375062006-03-10 07:25:12 +0000448 unsigned VReg = getVR(Op, VRBaseMap);
Chris Lattner749c6f62008-01-07 07:27:27 +0000449 const TargetInstrDesc &TID = MI->getDesc();
450 bool isOptDef = (IIOpNum < TID.getNumOperands())
451 ? (TID.OpInfo[IIOpNum].isOptionalDef()) : false;
Chris Lattner8019f412007-12-30 00:41:17 +0000452 MI->addOperand(MachineOperand::CreateReg(VReg, isOptDef));
Chris Lattnered18b682006-02-24 18:54:03 +0000453
454 // Verify that it is right.
Dan Gohman6f0d0242008-02-10 18:45:23 +0000455 assert(TargetRegisterInfo::isVirtualRegister(VReg) && "Not a vreg?");
Chris Lattnered18b682006-02-24 18:54:03 +0000456 if (II) {
Jim Laskey60f09922006-07-21 20:57:35 +0000457 const TargetRegisterClass *RC =
Dan Gohman6f0d0242008-02-10 18:45:23 +0000458 getInstrOperandRegClass(TRI, TII, *II, IIOpNum);
Evan Cheng21d03f22006-05-18 20:42:07 +0000459 assert(RC && "Don't have operand info for this instruction!");
Chris Lattner84bc5422007-12-31 04:13:23 +0000460 const TargetRegisterClass *VRC = RegInfo.getRegClass(VReg);
Chris Lattner01528292007-02-15 18:17:56 +0000461 if (VRC != RC) {
462 cerr << "Register class of operand and regclass of use don't agree!\n";
463#ifndef NDEBUG
464 cerr << "Operand = " << IIOpNum << "\n";
Chris Lattner95ad9432007-02-17 06:38:37 +0000465 cerr << "Op->Val = "; Op.Val->dump(&DAG); cerr << "\n";
Chris Lattner01528292007-02-15 18:17:56 +0000466 cerr << "MI = "; MI->print(cerr);
467 cerr << "VReg = " << VReg << "\n";
468 cerr << "VReg RegClass size = " << VRC->getSize()
Chris Lattner5d4a9f72007-02-15 18:19:15 +0000469 << ", align = " << VRC->getAlignment() << "\n";
Chris Lattner01528292007-02-15 18:17:56 +0000470 cerr << "Expected RegClass size = " << RC->getSize()
Chris Lattner5d4a9f72007-02-15 18:19:15 +0000471 << ", align = " << RC->getAlignment() << "\n";
Chris Lattner01528292007-02-15 18:17:56 +0000472#endif
473 cerr << "Fatal error, aborting.\n";
474 abort();
475 }
Chris Lattnered18b682006-02-24 18:54:03 +0000476 }
Chris Lattnerfec65d52007-12-30 00:51:11 +0000477 } else if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Chris Lattner8019f412007-12-30 00:41:17 +0000478 MI->addOperand(MachineOperand::CreateImm(C->getValue()));
Nate Begemane1795842008-02-14 08:57:00 +0000479 } else if (ConstantFPSDNode *F = dyn_cast<ConstantFPSDNode>(Op)) {
480 const Type *FType = MVT::getTypeForValueType(Op.getValueType());
481 ConstantFP *CFP = ConstantFP::get(FType, F->getValueAPF());
482 MI->addOperand(MachineOperand::CreateFPImm(CFP));
Chris Lattnerfec65d52007-12-30 00:51:11 +0000483 } else if (RegisterSDNode *R = dyn_cast<RegisterSDNode>(Op)) {
Chris Lattner8019f412007-12-30 00:41:17 +0000484 MI->addOperand(MachineOperand::CreateReg(R->getReg(), false));
Chris Lattnerfec65d52007-12-30 00:51:11 +0000485 } else if (GlobalAddressSDNode *TGA = dyn_cast<GlobalAddressSDNode>(Op)) {
486 MI->addOperand(MachineOperand::CreateGA(TGA->getGlobal(),TGA->getOffset()));
487 } else if (BasicBlockSDNode *BB = dyn_cast<BasicBlockSDNode>(Op)) {
488 MI->addOperand(MachineOperand::CreateMBB(BB->getBasicBlock()));
489 } else if (FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(Op)) {
490 MI->addOperand(MachineOperand::CreateFI(FI->getIndex()));
491 } else if (JumpTableSDNode *JT = dyn_cast<JumpTableSDNode>(Op)) {
492 MI->addOperand(MachineOperand::CreateJTI(JT->getIndex()));
493 } else if (ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(Op)) {
Evan Cheng404cb4f2006-02-25 09:54:52 +0000494 int Offset = CP->getOffset();
Chris Lattnered18b682006-02-24 18:54:03 +0000495 unsigned Align = CP->getAlignment();
Evan Chengd6594ae2006-09-12 21:00:35 +0000496 const Type *Type = CP->getType();
Chris Lattnered18b682006-02-24 18:54:03 +0000497 // MachineConstantPool wants an explicit alignment.
498 if (Align == 0) {
Evan Chengde268f72007-01-24 07:03:39 +0000499 Align = TM.getTargetData()->getPreferredTypeAlignmentShift(Type);
Evan Chengf6d039a2007-01-22 23:13:55 +0000500 if (Align == 0) {
Reid Spencerac9dcb92007-02-15 03:39:18 +0000501 // Alignment of vector types. FIXME!
Duncan Sands514ab342007-11-01 20:53:16 +0000502 Align = TM.getTargetData()->getABITypeSize(Type);
Evan Chengf6d039a2007-01-22 23:13:55 +0000503 Align = Log2_64(Align);
Chris Lattner54a30b92006-03-20 01:51:46 +0000504 }
Chris Lattnered18b682006-02-24 18:54:03 +0000505 }
506
Evan Chengd6594ae2006-09-12 21:00:35 +0000507 unsigned Idx;
508 if (CP->isMachineConstantPoolEntry())
509 Idx = ConstPool->getConstantPoolIndex(CP->getMachineCPVal(), Align);
510 else
511 Idx = ConstPool->getConstantPoolIndex(CP->getConstVal(), Align);
Chris Lattnerfec65d52007-12-30 00:51:11 +0000512 MI->addOperand(MachineOperand::CreateCPI(Idx, Offset));
513 } else if (ExternalSymbolSDNode *ES = dyn_cast<ExternalSymbolSDNode>(Op)) {
514 MI->addOperand(MachineOperand::CreateES(ES->getSymbol()));
Chris Lattnered18b682006-02-24 18:54:03 +0000515 } else {
516 assert(Op.getValueType() != MVT::Other &&
517 Op.getValueType() != MVT::Flag &&
518 "Chain and flag operands should occur at end of operand list!");
Chris Lattnerdf375062006-03-10 07:25:12 +0000519 unsigned VReg = getVR(Op, VRBaseMap);
Chris Lattner8019f412007-12-30 00:41:17 +0000520 MI->addOperand(MachineOperand::CreateReg(VReg, false));
Chris Lattnered18b682006-02-24 18:54:03 +0000521
522 // Verify that it is right.
Dan Gohman6f0d0242008-02-10 18:45:23 +0000523 assert(TargetRegisterInfo::isVirtualRegister(VReg) && "Not a vreg?");
Chris Lattnered18b682006-02-24 18:54:03 +0000524 if (II) {
Jim Laskey60f09922006-07-21 20:57:35 +0000525 const TargetRegisterClass *RC =
Dan Gohman6f0d0242008-02-10 18:45:23 +0000526 getInstrOperandRegClass(TRI, TII, *II, IIOpNum);
Evan Cheng21d03f22006-05-18 20:42:07 +0000527 assert(RC && "Don't have operand info for this instruction!");
Chris Lattner84bc5422007-12-31 04:13:23 +0000528 assert(RegInfo.getRegClass(VReg) == RC &&
Chris Lattnered18b682006-02-24 18:54:03 +0000529 "Register class of operand and regclass of use don't agree!");
530 }
531 }
532
533}
534
Dan Gohman69de1932008-02-06 22:27:42 +0000535void ScheduleDAG::AddMemOperand(MachineInstr *MI, const MemOperand &MO) {
536 MI->addMemOperand(MO);
537}
538
Christopher Lambe24f8f12007-07-26 08:12:07 +0000539// Returns the Register Class of a subregister
540static const TargetRegisterClass *getSubRegisterRegClass(
541 const TargetRegisterClass *TRC,
542 unsigned SubIdx) {
543 // Pick the register class of the subregister
Dan Gohman6f0d0242008-02-10 18:45:23 +0000544 TargetRegisterInfo::regclass_iterator I =
545 TRC->subregclasses_begin() + SubIdx-1;
Christopher Lambe24f8f12007-07-26 08:12:07 +0000546 assert(I < TRC->subregclasses_end() &&
547 "Invalid subregister index for register class");
548 return *I;
549}
550
551static const TargetRegisterClass *getSuperregRegisterClass(
552 const TargetRegisterClass *TRC,
553 unsigned SubIdx,
554 MVT::ValueType VT) {
555 // Pick the register class of the superegister for this type
Dan Gohman6f0d0242008-02-10 18:45:23 +0000556 for (TargetRegisterInfo::regclass_iterator I = TRC->superregclasses_begin(),
Christopher Lambe24f8f12007-07-26 08:12:07 +0000557 E = TRC->superregclasses_end(); I != E; ++I)
558 if ((*I)->hasType(VT) && getSubRegisterRegClass(*I, SubIdx) == TRC)
559 return *I;
560 assert(false && "Couldn't find the register class");
561 return 0;
562}
563
564/// EmitSubregNode - Generate machine code for subreg nodes.
565///
566void ScheduleDAG::EmitSubregNode(SDNode *Node,
567 DenseMap<SDOperand, unsigned> &VRBaseMap) {
568 unsigned VRBase = 0;
569 unsigned Opc = Node->getTargetOpcode();
570 if (Opc == TargetInstrInfo::EXTRACT_SUBREG) {
571 // If the node is only used by a CopyToReg and the dest reg is a vreg, use
572 // the CopyToReg'd destination register instead of creating a new vreg.
573 for (SDNode::use_iterator UI = Node->use_begin(), E = Node->use_end();
574 UI != E; ++UI) {
575 SDNode *Use = *UI;
576 if (Use->getOpcode() == ISD::CopyToReg &&
577 Use->getOperand(2).Val == Node) {
578 unsigned DestReg = cast<RegisterSDNode>(Use->getOperand(1))->getReg();
Dan Gohman6f0d0242008-02-10 18:45:23 +0000579 if (TargetRegisterInfo::isVirtualRegister(DestReg)) {
Christopher Lambe24f8f12007-07-26 08:12:07 +0000580 VRBase = DestReg;
581 break;
582 }
583 }
584 }
585
586 unsigned SubIdx = cast<ConstantSDNode>(Node->getOperand(1))->getValue();
587
588 // TODO: If the node is a use of a CopyFromReg from a physical register
589 // fold the extract into the copy now
590
Christopher Lambe24f8f12007-07-26 08:12:07 +0000591 // Create the extract_subreg machine instruction.
592 MachineInstr *MI =
593 new MachineInstr(BB, TII->get(TargetInstrInfo::EXTRACT_SUBREG));
594
595 // Figure out the register class to create for the destreg.
596 unsigned VReg = getVR(Node->getOperand(0), VRBaseMap);
Chris Lattner84bc5422007-12-31 04:13:23 +0000597 const TargetRegisterClass *TRC = RegInfo.getRegClass(VReg);
Christopher Lambe24f8f12007-07-26 08:12:07 +0000598 const TargetRegisterClass *SRC = getSubRegisterRegClass(TRC, SubIdx);
599
600 if (VRBase) {
601 // Grab the destination register
602 const TargetRegisterClass *DRC = 0;
Chris Lattner84bc5422007-12-31 04:13:23 +0000603 DRC = RegInfo.getRegClass(VRBase);
Christopher Lamb175e8152008-01-31 07:09:08 +0000604 assert(SRC && DRC && SRC == DRC &&
Christopher Lambe24f8f12007-07-26 08:12:07 +0000605 "Source subregister and destination must have the same class");
606 } else {
607 // Create the reg
Christopher Lamb175e8152008-01-31 07:09:08 +0000608 assert(SRC && "Couldn't find source register class");
Chris Lattner84bc5422007-12-31 04:13:23 +0000609 VRBase = RegInfo.createVirtualRegister(SRC);
Christopher Lambe24f8f12007-07-26 08:12:07 +0000610 }
611
612 // Add def, source, and subreg index
Chris Lattner8019f412007-12-30 00:41:17 +0000613 MI->addOperand(MachineOperand::CreateReg(VRBase, true));
Christopher Lambe24f8f12007-07-26 08:12:07 +0000614 AddOperand(MI, Node->getOperand(0), 0, 0, VRBaseMap);
Chris Lattnerfec65d52007-12-30 00:51:11 +0000615 MI->addOperand(MachineOperand::CreateImm(SubIdx));
Christopher Lambe24f8f12007-07-26 08:12:07 +0000616
617 } else if (Opc == TargetInstrInfo::INSERT_SUBREG) {
618 assert((Node->getNumOperands() == 2 || Node->getNumOperands() == 3) &&
619 "Malformed insert_subreg node");
620 bool isUndefInput = (Node->getNumOperands() == 2);
621 unsigned SubReg = 0;
622 unsigned SubIdx = 0;
623
624 if (isUndefInput) {
625 SubReg = getVR(Node->getOperand(0), VRBaseMap);
626 SubIdx = cast<ConstantSDNode>(Node->getOperand(1))->getValue();
627 } else {
628 SubReg = getVR(Node->getOperand(1), VRBaseMap);
629 SubIdx = cast<ConstantSDNode>(Node->getOperand(2))->getValue();
630 }
631
Chris Lattner534bcfb2007-12-31 04:16:08 +0000632 // TODO: Add tracking info to MachineRegisterInfo of which vregs are subregs
Christopher Lambe24f8f12007-07-26 08:12:07 +0000633 // to allow coalescing in the allocator
634
635 // If the node is only used by a CopyToReg and the dest reg is a vreg, use
636 // the CopyToReg'd destination register instead of creating a new vreg.
637 // If the CopyToReg'd destination register is physical, then fold the
638 // insert into the copy
639 for (SDNode::use_iterator UI = Node->use_begin(), E = Node->use_end();
640 UI != E; ++UI) {
641 SDNode *Use = *UI;
642 if (Use->getOpcode() == ISD::CopyToReg &&
643 Use->getOperand(2).Val == Node) {
644 unsigned DestReg = cast<RegisterSDNode>(Use->getOperand(1))->getReg();
Dan Gohman6f0d0242008-02-10 18:45:23 +0000645 if (TargetRegisterInfo::isVirtualRegister(DestReg)) {
Christopher Lambe24f8f12007-07-26 08:12:07 +0000646 VRBase = DestReg;
647 break;
648 }
649 }
650 }
651
652 // Create the insert_subreg machine instruction.
653 MachineInstr *MI =
654 new MachineInstr(BB, TII->get(TargetInstrInfo::INSERT_SUBREG));
655
656 // Figure out the register class to create for the destreg.
657 const TargetRegisterClass *TRC = 0;
658 if (VRBase) {
Chris Lattner84bc5422007-12-31 04:13:23 +0000659 TRC = RegInfo.getRegClass(VRBase);
Christopher Lambe24f8f12007-07-26 08:12:07 +0000660 } else {
Chris Lattner84bc5422007-12-31 04:13:23 +0000661 TRC = getSuperregRegisterClass(RegInfo.getRegClass(SubReg), SubIdx,
Christopher Lambe24f8f12007-07-26 08:12:07 +0000662 Node->getValueType(0));
663 assert(TRC && "Couldn't determine register class for insert_subreg");
Chris Lattner84bc5422007-12-31 04:13:23 +0000664 VRBase = RegInfo.createVirtualRegister(TRC); // Create the reg
Christopher Lambe24f8f12007-07-26 08:12:07 +0000665 }
666
Chris Lattner8019f412007-12-30 00:41:17 +0000667 MI->addOperand(MachineOperand::CreateReg(VRBase, true));
Christopher Lambe24f8f12007-07-26 08:12:07 +0000668 AddOperand(MI, Node->getOperand(0), 0, 0, VRBaseMap);
669 if (!isUndefInput)
670 AddOperand(MI, Node->getOperand(1), 0, 0, VRBaseMap);
Chris Lattnerfec65d52007-12-30 00:51:11 +0000671 MI->addOperand(MachineOperand::CreateImm(SubIdx));
Christopher Lambe24f8f12007-07-26 08:12:07 +0000672 } else
673 assert(0 && "Node is not a subreg insert or extract");
674
675 bool isNew = VRBaseMap.insert(std::make_pair(SDOperand(Node,0), VRBase));
676 assert(isNew && "Node emitted out of order - early");
677}
678
Jim Laskeyb6d4c2c2005-09-30 19:15:27 +0000679/// EmitNode - Generate machine code for an node and needed dependencies.
Jim Laskeye6b90fb2005-09-26 21:57:04 +0000680///
Evan Chenga6fb1b62007-09-25 01:54:36 +0000681void ScheduleDAG::EmitNode(SDNode *Node, unsigned InstanceNo,
Evan Chengaf825c82007-07-10 07:08:32 +0000682 DenseMap<SDOperand, unsigned> &VRBaseMap) {
Jim Laskeyb6d4c2c2005-09-30 19:15:27 +0000683 // If machine instruction
684 if (Node->isTargetOpcode()) {
685 unsigned Opc = Node->getTargetOpcode();
Christopher Lambe24f8f12007-07-26 08:12:07 +0000686
687 // Handle subreg insert/extract specially
688 if (Opc == TargetInstrInfo::EXTRACT_SUBREG ||
689 Opc == TargetInstrInfo::INSERT_SUBREG) {
690 EmitSubregNode(Node, VRBaseMap);
691 return;
692 }
693
Chris Lattner749c6f62008-01-07 07:27:27 +0000694 const TargetInstrDesc &II = TII->get(Opc);
Chris Lattner2d973e42005-08-18 20:07:59 +0000695
Jim Laskeyb6d4c2c2005-09-30 19:15:27 +0000696 unsigned NumResults = CountResults(Node);
697 unsigned NodeOperands = CountOperands(Node);
Dan Gohman42a77882008-02-16 00:36:48 +0000698 unsigned MemOperandsEnd = ComputeMemOperandsEnd(Node);
Jim Laskeye6b90fb2005-09-26 21:57:04 +0000699 unsigned NumMIOperands = NodeOperands + NumResults;
Chris Lattner349c4952008-01-07 03:13:06 +0000700 bool HasPhysRegOuts = (NumResults > II.getNumDefs()) &&
701 II.getImplicitDefs() != 0;
Chris Lattnerda8abb02005-09-01 18:44:10 +0000702#ifndef NDEBUG
Chris Lattner349c4952008-01-07 03:13:06 +0000703 assert((II.getNumOperands() == NumMIOperands ||
Chris Lattner8f707e12008-01-07 05:19:29 +0000704 HasPhysRegOuts || II.isVariadic()) &&
Chris Lattner2d973e42005-08-18 20:07:59 +0000705 "#operands for dag node doesn't match .td file!");
Chris Lattnerca6aa2f2005-08-19 01:01:34 +0000706#endif
Chris Lattner2d973e42005-08-18 20:07:59 +0000707
708 // Create the new machine instruction.
Evan Chengc0f64ff2006-11-27 23:37:22 +0000709 MachineInstr *MI = new MachineInstr(II);
Chris Lattner2d973e42005-08-18 20:07:59 +0000710
711 // Add result register values for things that are defined by this
712 // instruction.
Evan Chengaf825c82007-07-10 07:08:32 +0000713 if (NumResults)
Evan Cheng84097472007-08-02 00:28:15 +0000714 CreateVirtualRegisters(Node, MI, II, VRBaseMap);
Jim Laskeyb6d4c2c2005-09-30 19:15:27 +0000715
716 // Emit all of the actual operands of this instruction, adding them to the
717 // instruction as appropriate.
Chris Lattnered18b682006-02-24 18:54:03 +0000718 for (unsigned i = 0; i != NodeOperands; ++i)
Chris Lattner349c4952008-01-07 03:13:06 +0000719 AddOperand(MI, Node->getOperand(i), i+II.getNumDefs(), &II, VRBaseMap);
Evan Cheng13d41b92006-05-12 01:58:24 +0000720
Dan Gohman69de1932008-02-06 22:27:42 +0000721 // Emit all of the memory operands of this instruction
Dan Gohman42a77882008-02-16 00:36:48 +0000722 for (unsigned i = NodeOperands; i != MemOperandsEnd; ++i)
Dan Gohman69de1932008-02-06 22:27:42 +0000723 AddMemOperand(MI, cast<MemOperandSDNode>(Node->getOperand(i))->MO);
724
Evan Cheng13d41b92006-05-12 01:58:24 +0000725 // Commute node if it has been determined to be profitable.
726 if (CommuteSet.count(Node)) {
727 MachineInstr *NewMI = TII->commuteInstruction(MI);
728 if (NewMI == 0)
Bill Wendling832171c2006-12-07 20:04:42 +0000729 DOUT << "Sched: COMMUTING FAILED!\n";
Evan Cheng13d41b92006-05-12 01:58:24 +0000730 else {
Bill Wendling832171c2006-12-07 20:04:42 +0000731 DOUT << "Sched: COMMUTED TO: " << *NewMI;
Evan Cheng4c6f2f92006-05-31 18:03:39 +0000732 if (MI != NewMI) {
733 delete MI;
734 MI = NewMI;
735 }
Evan Cheng13d41b92006-05-12 01:58:24 +0000736 }
737 }
738
Evan Cheng1b08bbc2008-02-01 09:10:45 +0000739 if (II.usesCustomDAGSchedInsertionHook())
Evan Cheng6b2cf282008-01-30 19:35:32 +0000740 // Insert this instruction into the basic block using a target
741 // specific inserter which may returns a new basic block.
Evan Chengff9b3732008-01-30 18:18:23 +0000742 BB = DAG.getTargetLoweringInfo().EmitInstrWithCustomInserter(MI, BB);
Evan Cheng6b2cf282008-01-30 19:35:32 +0000743 else
744 BB->push_back(MI);
Evan Cheng84097472007-08-02 00:28:15 +0000745
746 // Additional results must be an physical register def.
747 if (HasPhysRegOuts) {
Chris Lattner349c4952008-01-07 03:13:06 +0000748 for (unsigned i = II.getNumDefs(); i < NumResults; ++i) {
749 unsigned Reg = II.getImplicitDefs()[i - II.getNumDefs()];
Evan Cheng33d55952007-08-02 05:29:38 +0000750 if (Node->hasAnyUseOfValue(i))
Evan Chenga6fb1b62007-09-25 01:54:36 +0000751 EmitCopyFromReg(Node, i, InstanceNo, Reg, VRBaseMap);
Evan Cheng84097472007-08-02 00:28:15 +0000752 }
753 }
Jim Laskeyb6d4c2c2005-09-30 19:15:27 +0000754 } else {
755 switch (Node->getOpcode()) {
756 default:
Jim Laskey16d42c62006-07-11 18:25:13 +0000757#ifndef NDEBUG
Dan Gohmanb5bec2b2007-06-19 14:13:56 +0000758 Node->dump(&DAG);
Jim Laskey16d42c62006-07-11 18:25:13 +0000759#endif
Jim Laskeyb6d4c2c2005-09-30 19:15:27 +0000760 assert(0 && "This target-independent node should have been selected!");
761 case ISD::EntryToken: // fall thru
762 case ISD::TokenFactor:
Jim Laskey1ee29252007-01-26 14:34:52 +0000763 case ISD::LABEL:
Evan Chenga844bde2008-02-02 04:07:54 +0000764 case ISD::DECLARE:
Dan Gohman69de1932008-02-06 22:27:42 +0000765 case ISD::SRCVALUE:
Jim Laskeyb6d4c2c2005-09-30 19:15:27 +0000766 break;
767 case ISD::CopyToReg: {
Evan Cheng489a87c2007-01-05 20:59:06 +0000768 unsigned InReg;
769 if (RegisterSDNode *R = dyn_cast<RegisterSDNode>(Node->getOperand(2)))
770 InReg = R->getReg();
771 else
772 InReg = getVR(Node->getOperand(2), VRBaseMap);
Chris Lattnera4176522005-10-30 18:54:27 +0000773 unsigned DestReg = cast<RegisterSDNode>(Node->getOperand(1))->getReg();
Lauro Ramos Venancio8334b9f2007-03-20 16:46:44 +0000774 if (InReg != DestReg) {// Coalesced away the copy?
775 const TargetRegisterClass *TRC = 0;
776 // Get the target register class
Dan Gohman6f0d0242008-02-10 18:45:23 +0000777 if (TargetRegisterInfo::isVirtualRegister(InReg))
Chris Lattner84bc5422007-12-31 04:13:23 +0000778 TRC = RegInfo.getRegClass(InReg);
Lauro Ramos Venancioa0a26b72007-03-20 20:09:03 +0000779 else
Evan Cheng42d60272007-09-26 21:36:17 +0000780 TRC =
Dan Gohman6f0d0242008-02-10 18:45:23 +0000781 TRI->getPhysicalRegisterRegClass(Node->getOperand(2).getValueType(),
Lauro Ramos Venancioa0a26b72007-03-20 20:09:03 +0000782 InReg);
Owen Andersond10fd972007-12-31 06:32:00 +0000783 TII->copyRegToReg(*BB, BB->end(), DestReg, InReg, TRC, TRC);
Lauro Ramos Venancio8334b9f2007-03-20 16:46:44 +0000784 }
Jim Laskeyb6d4c2c2005-09-30 19:15:27 +0000785 break;
786 }
787 case ISD::CopyFromReg: {
788 unsigned SrcReg = cast<RegisterSDNode>(Node->getOperand(1))->getReg();
Evan Chenga6fb1b62007-09-25 01:54:36 +0000789 EmitCopyFromReg(Node, 0, InstanceNo, SrcReg, VRBaseMap);
Jim Laskeyb6d4c2c2005-09-30 19:15:27 +0000790 break;
791 }
Chris Lattneracc43bf2006-01-26 23:28:04 +0000792 case ISD::INLINEASM: {
793 unsigned NumOps = Node->getNumOperands();
794 if (Node->getOperand(NumOps-1).getValueType() == MVT::Flag)
795 --NumOps; // Ignore the flag operand.
796
797 // Create the inline asm machine instruction.
798 MachineInstr *MI =
Evan Chengc0f64ff2006-11-27 23:37:22 +0000799 new MachineInstr(BB, TII->get(TargetInstrInfo::INLINEASM));
Chris Lattneracc43bf2006-01-26 23:28:04 +0000800
801 // Add the asm string as an external symbol operand.
802 const char *AsmStr =
803 cast<ExternalSymbolSDNode>(Node->getOperand(1))->getSymbol();
Chris Lattnerfec65d52007-12-30 00:51:11 +0000804 MI->addOperand(MachineOperand::CreateES(AsmStr));
Chris Lattneracc43bf2006-01-26 23:28:04 +0000805
806 // Add all of the operand registers to the instruction.
Chris Lattnerc3a9f8d2006-02-23 19:21:04 +0000807 for (unsigned i = 2; i != NumOps;) {
808 unsigned Flags = cast<ConstantSDNode>(Node->getOperand(i))->getValue();
Chris Lattnerfd6d2822006-02-24 19:18:20 +0000809 unsigned NumVals = Flags >> 3;
Chris Lattnerc3a9f8d2006-02-23 19:21:04 +0000810
Chris Lattnerfec65d52007-12-30 00:51:11 +0000811 MI->addOperand(MachineOperand::CreateImm(Flags));
Chris Lattnerc3a9f8d2006-02-23 19:21:04 +0000812 ++i; // Skip the ID value.
813
814 switch (Flags & 7) {
Chris Lattneracc43bf2006-01-26 23:28:04 +0000815 default: assert(0 && "Bad flags!");
Chris Lattnerc3a9f8d2006-02-23 19:21:04 +0000816 case 1: // Use of register.
Chris Lattnerfd6d2822006-02-24 19:18:20 +0000817 for (; NumVals; --NumVals, ++i) {
Chris Lattnerc3a9f8d2006-02-23 19:21:04 +0000818 unsigned Reg = cast<RegisterSDNode>(Node->getOperand(i))->getReg();
Chris Lattner8019f412007-12-30 00:41:17 +0000819 MI->addOperand(MachineOperand::CreateReg(Reg, false));
Chris Lattnerc3a9f8d2006-02-23 19:21:04 +0000820 }
Chris Lattnerdc19b702006-02-04 02:26:14 +0000821 break;
Chris Lattnerc3a9f8d2006-02-23 19:21:04 +0000822 case 2: // Def of register.
Chris Lattnerfd6d2822006-02-24 19:18:20 +0000823 for (; NumVals; --NumVals, ++i) {
Chris Lattnerc3a9f8d2006-02-23 19:21:04 +0000824 unsigned Reg = cast<RegisterSDNode>(Node->getOperand(i))->getReg();
Chris Lattner8019f412007-12-30 00:41:17 +0000825 MI->addOperand(MachineOperand::CreateReg(Reg, true));
Chris Lattnerc3a9f8d2006-02-23 19:21:04 +0000826 }
Chris Lattnerdc19b702006-02-04 02:26:14 +0000827 break;
Chris Lattnerdc19b702006-02-04 02:26:14 +0000828 case 3: { // Immediate.
Chris Lattner7df31dc2007-08-25 00:53:07 +0000829 for (; NumVals; --NumVals, ++i) {
830 if (ConstantSDNode *CS =
831 dyn_cast<ConstantSDNode>(Node->getOperand(i))) {
Chris Lattner8019f412007-12-30 00:41:17 +0000832 MI->addOperand(MachineOperand::CreateImm(CS->getValue()));
Dale Johanneseneb57ea72007-11-05 21:20:28 +0000833 } else if (GlobalAddressSDNode *GA =
834 dyn_cast<GlobalAddressSDNode>(Node->getOperand(i))) {
Chris Lattnerfec65d52007-12-30 00:51:11 +0000835 MI->addOperand(MachineOperand::CreateGA(GA->getGlobal(),
836 GA->getOffset()));
Dale Johanneseneb57ea72007-11-05 21:20:28 +0000837 } else {
Chris Lattnerfec65d52007-12-30 00:51:11 +0000838 BasicBlockSDNode *BB =cast<BasicBlockSDNode>(Node->getOperand(i));
839 MI->addOperand(MachineOperand::CreateMBB(BB->getBasicBlock()));
Chris Lattner7df31dc2007-08-25 00:53:07 +0000840 }
Chris Lattnerefa46ce2006-10-31 20:01:56 +0000841 }
Chris Lattnerdc19b702006-02-04 02:26:14 +0000842 break;
843 }
Chris Lattnerfd6d2822006-02-24 19:18:20 +0000844 case 4: // Addressing mode.
845 // The addressing mode has been selected, just add all of the
846 // operands to the machine instruction.
847 for (; NumVals; --NumVals, ++i)
Chris Lattnerdf375062006-03-10 07:25:12 +0000848 AddOperand(MI, Node->getOperand(i), 0, 0, VRBaseMap);
Chris Lattnerfd6d2822006-02-24 19:18:20 +0000849 break;
Chris Lattnerdc19b702006-02-04 02:26:14 +0000850 }
Chris Lattneracc43bf2006-01-26 23:28:04 +0000851 }
852 break;
853 }
Jim Laskeyb6d4c2c2005-09-30 19:15:27 +0000854 }
855 }
Jim Laskeyb6d4c2c2005-09-30 19:15:27 +0000856}
857
Chris Lattnera93dfcd2006-03-05 23:51:47 +0000858void ScheduleDAG::EmitNoop() {
859 TII->insertNoop(*BB, BB->end());
860}
861
Evan Cheng42d60272007-09-26 21:36:17 +0000862void ScheduleDAG::EmitCrossRCCopy(SUnit *SU, DenseMap<SUnit*, unsigned> &VRBaseMap) {
863 for (SUnit::const_pred_iterator I = SU->Preds.begin(), E = SU->Preds.end();
864 I != E; ++I) {
865 if (I->isCtrl) continue; // ignore chain preds
866 if (!I->Dep->Node) {
867 // Copy to physical register.
868 DenseMap<SUnit*, unsigned>::iterator VRI = VRBaseMap.find(I->Dep);
869 assert(VRI != VRBaseMap.end() && "Node emitted out of order - late");
870 // Find the destination physical register.
871 unsigned Reg = 0;
872 for (SUnit::const_succ_iterator II = SU->Succs.begin(),
873 EE = SU->Succs.end(); II != EE; ++II) {
874 if (I->Reg) {
875 Reg = I->Reg;
876 break;
877 }
878 }
879 assert(I->Reg && "Unknown physical register!");
Owen Andersond10fd972007-12-31 06:32:00 +0000880 TII->copyRegToReg(*BB, BB->end(), Reg, VRI->second,
Evan Cheng42d60272007-09-26 21:36:17 +0000881 SU->CopyDstRC, SU->CopySrcRC);
882 } else {
883 // Copy from physical register.
884 assert(I->Reg && "Unknown physical register!");
Chris Lattner84bc5422007-12-31 04:13:23 +0000885 unsigned VRBase = RegInfo.createVirtualRegister(SU->CopyDstRC);
Evan Cheng42d60272007-09-26 21:36:17 +0000886 bool isNew = VRBaseMap.insert(std::make_pair(SU, VRBase));
887 assert(isNew && "Node emitted out of order - early");
Owen Andersond10fd972007-12-31 06:32:00 +0000888 TII->copyRegToReg(*BB, BB->end(), VRBase, I->Reg,
Evan Cheng42d60272007-09-26 21:36:17 +0000889 SU->CopyDstRC, SU->CopySrcRC);
890 }
891 break;
892 }
893}
894
Evan Chenge165a782006-05-11 23:55:42 +0000895/// EmitSchedule - Emit the machine code in scheduled order.
896void ScheduleDAG::EmitSchedule() {
Chris Lattner96645412006-05-16 06:10:58 +0000897 // If this is the first basic block in the function, and if it has live ins
898 // that need to be copied into vregs, emit the copies into the top of the
899 // block before emitting the code for the block.
Evan Cheng6b2cf282008-01-30 19:35:32 +0000900 if (&MF->front() == BB) {
Chris Lattner84bc5422007-12-31 04:13:23 +0000901 for (MachineRegisterInfo::livein_iterator LI = RegInfo.livein_begin(),
902 E = RegInfo.livein_end(); LI != E; ++LI)
Evan Cheng9efce632007-09-26 06:25:56 +0000903 if (LI->second) {
Chris Lattner84bc5422007-12-31 04:13:23 +0000904 const TargetRegisterClass *RC = RegInfo.getRegClass(LI->second);
Evan Cheng6b2cf282008-01-30 19:35:32 +0000905 TII->copyRegToReg(*MF->begin(), MF->begin()->end(), LI->second,
Evan Cheng9efce632007-09-26 06:25:56 +0000906 LI->first, RC, RC);
907 }
Chris Lattner96645412006-05-16 06:10:58 +0000908 }
909
910
911 // Finally, emit the code for all of the scheduled instructions.
Evan Chengaf825c82007-07-10 07:08:32 +0000912 DenseMap<SDOperand, unsigned> VRBaseMap;
Evan Cheng42d60272007-09-26 21:36:17 +0000913 DenseMap<SUnit*, unsigned> CopyVRBaseMap;
Evan Chenge165a782006-05-11 23:55:42 +0000914 for (unsigned i = 0, e = Sequence.size(); i != e; i++) {
915 if (SUnit *SU = Sequence[i]) {
Evan Chenga6fb1b62007-09-25 01:54:36 +0000916 for (unsigned j = 0, ee = SU->FlaggedNodes.size(); j != ee; ++j)
917 EmitNode(SU->FlaggedNodes[j], SU->InstanceNo, VRBaseMap);
Evan Cheng42d60272007-09-26 21:36:17 +0000918 if (SU->Node)
919 EmitNode(SU->Node, SU->InstanceNo, VRBaseMap);
920 else
921 EmitCrossRCCopy(SU, CopyVRBaseMap);
Evan Chenge165a782006-05-11 23:55:42 +0000922 } else {
923 // Null SUnit* is a noop.
924 EmitNoop();
925 }
926 }
927}
928
929/// dump - dump the schedule.
930void ScheduleDAG::dumpSchedule() const {
931 for (unsigned i = 0, e = Sequence.size(); i != e; i++) {
932 if (SUnit *SU = Sequence[i])
933 SU->dump(&DAG);
934 else
Bill Wendling832171c2006-12-07 20:04:42 +0000935 cerr << "**** NOOP ****\n";
Evan Chenge165a782006-05-11 23:55:42 +0000936 }
937}
938
939
Evan Chenga9c20912006-01-21 02:32:06 +0000940/// Run - perform scheduling.
941///
942MachineBasicBlock *ScheduleDAG::Run() {
Evan Chenga9c20912006-01-21 02:32:06 +0000943 Schedule();
944 return BB;
Chris Lattnerd32b2362005-08-18 18:45:24 +0000945}
Evan Cheng4ef10862006-01-23 07:01:07 +0000946
Evan Chenge165a782006-05-11 23:55:42 +0000947/// SUnit - Scheduling unit. It's an wrapper around either a single SDNode or
948/// a group of nodes flagged together.
949void SUnit::dump(const SelectionDAG *G) const {
Bill Wendling832171c2006-12-07 20:04:42 +0000950 cerr << "SU(" << NodeNum << "): ";
Evan Cheng42d60272007-09-26 21:36:17 +0000951 if (Node)
952 Node->dump(G);
953 else
954 cerr << "CROSS RC COPY ";
Bill Wendling832171c2006-12-07 20:04:42 +0000955 cerr << "\n";
Evan Chenge165a782006-05-11 23:55:42 +0000956 if (FlaggedNodes.size() != 0) {
957 for (unsigned i = 0, e = FlaggedNodes.size(); i != e; i++) {
Bill Wendling832171c2006-12-07 20:04:42 +0000958 cerr << " ";
Evan Chenge165a782006-05-11 23:55:42 +0000959 FlaggedNodes[i]->dump(G);
Bill Wendling832171c2006-12-07 20:04:42 +0000960 cerr << "\n";
Evan Chenge165a782006-05-11 23:55:42 +0000961 }
962 }
963}
Evan Cheng4ef10862006-01-23 07:01:07 +0000964
Evan Chenge165a782006-05-11 23:55:42 +0000965void SUnit::dumpAll(const SelectionDAG *G) const {
966 dump(G);
967
Bill Wendling832171c2006-12-07 20:04:42 +0000968 cerr << " # preds left : " << NumPredsLeft << "\n";
969 cerr << " # succs left : " << NumSuccsLeft << "\n";
Bill Wendling832171c2006-12-07 20:04:42 +0000970 cerr << " Latency : " << Latency << "\n";
971 cerr << " Depth : " << Depth << "\n";
972 cerr << " Height : " << Height << "\n";
Evan Chenge165a782006-05-11 23:55:42 +0000973
974 if (Preds.size() != 0) {
Bill Wendling832171c2006-12-07 20:04:42 +0000975 cerr << " Predecessors:\n";
Chris Lattner228a18e2006-08-17 00:09:56 +0000976 for (SUnit::const_succ_iterator I = Preds.begin(), E = Preds.end();
977 I != E; ++I) {
Evan Cheng713a98d2007-09-19 01:38:40 +0000978 if (I->isCtrl)
Bill Wendling832171c2006-12-07 20:04:42 +0000979 cerr << " ch #";
Evan Chenge165a782006-05-11 23:55:42 +0000980 else
Bill Wendling832171c2006-12-07 20:04:42 +0000981 cerr << " val #";
Evan Chenga6fb1b62007-09-25 01:54:36 +0000982 cerr << I->Dep << " - SU(" << I->Dep->NodeNum << ")";
983 if (I->isSpecial)
984 cerr << " *";
985 cerr << "\n";
Evan Chenge165a782006-05-11 23:55:42 +0000986 }
987 }
988 if (Succs.size() != 0) {
Bill Wendling832171c2006-12-07 20:04:42 +0000989 cerr << " Successors:\n";
Chris Lattner228a18e2006-08-17 00:09:56 +0000990 for (SUnit::const_succ_iterator I = Succs.begin(), E = Succs.end();
991 I != E; ++I) {
Evan Cheng713a98d2007-09-19 01:38:40 +0000992 if (I->isCtrl)
Bill Wendling832171c2006-12-07 20:04:42 +0000993 cerr << " ch #";
Evan Chenge165a782006-05-11 23:55:42 +0000994 else
Bill Wendling832171c2006-12-07 20:04:42 +0000995 cerr << " val #";
Evan Chenga6fb1b62007-09-25 01:54:36 +0000996 cerr << I->Dep << " - SU(" << I->Dep->NodeNum << ")";
997 if (I->isSpecial)
998 cerr << " *";
999 cerr << "\n";
Evan Chenge165a782006-05-11 23:55:42 +00001000 }
1001 }
Bill Wendling832171c2006-12-07 20:04:42 +00001002 cerr << "\n";
Evan Chenge165a782006-05-11 23:55:42 +00001003}