blob: ca256deeaf7a96a3a18d56aa1a41e8cb10e71cbe [file] [log] [blame]
Evan Chenga9c20912006-01-21 02:32:06 +00001//===---- ScheduleDAG.cpp - Implement the ScheduleDAG class ---------------===//
Chris Lattnerd32b2362005-08-18 18:45:24 +00002//
3// The LLVM Compiler Infrastructure
4//
Jim Laskey5a608dd2005-10-31 12:49:09 +00005// This file was developed by James M. Laskey and is distributed under the
Chris Lattnerd32b2362005-08-18 18:45:24 +00006// University of Illinois Open Source License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
Jim Laskeye6b90fb2005-09-26 21:57:04 +000010// This implements a simple two pass scheduler. The first pass attempts to push
11// backward any lengthy instructions and critical paths. The second pass packs
12// instructions into semi-optimal time slots.
Chris Lattnerd32b2362005-08-18 18:45:24 +000013//
14//===----------------------------------------------------------------------===//
15
Evan Chenge165a782006-05-11 23:55:42 +000016#define DEBUG_TYPE "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 Chenge165a782006-05-11 23:55:42 +000030/// BuildSchedUnits - Build SUnits from the selection dag that we are input.
31/// This SUnit graph is similar to the SelectionDAG, but represents flagged
32/// together nodes with a single SUnit.
33void ScheduleDAG::BuildSchedUnits() {
34 // Reserve entries in the vector for each of the SUnits we are creating. This
35 // ensure that reallocation of the vector won't happen, so SUnit*'s won't get
36 // invalidated.
37 SUnits.reserve(std::distance(DAG.allnodes_begin(), DAG.allnodes_end()));
38
39 const InstrItineraryData &InstrItins = TM.getInstrItineraryData();
40
41 for (SelectionDAG::allnodes_iterator NI = DAG.allnodes_begin(),
42 E = DAG.allnodes_end(); NI != E; ++NI) {
43 if (isPassiveNode(NI)) // Leaf node, e.g. a TargetImmediate.
44 continue;
45
46 // If this node has already been processed, stop now.
47 if (SUnitMap[NI]) continue;
48
49 SUnit *NodeSUnit = NewSUnit(NI);
50
51 // See if anything is flagged to this node, if so, add them to flagged
52 // nodes. Nodes can have at most one flag input and one flag output. Flags
53 // are required the be the last operand and result of a node.
54
55 // Scan up, adding flagged preds to FlaggedNodes.
56 SDNode *N = NI;
Evan Cheng3b97acd2006-08-07 22:12:12 +000057 if (N->getNumOperands() &&
58 N->getOperand(N->getNumOperands()-1).getValueType() == MVT::Flag) {
59 do {
60 N = N->getOperand(N->getNumOperands()-1).Val;
61 NodeSUnit->FlaggedNodes.push_back(N);
62 SUnitMap[N] = NodeSUnit;
63 } while (N->getNumOperands() &&
64 N->getOperand(N->getNumOperands()-1).getValueType()== MVT::Flag);
65 std::reverse(NodeSUnit->FlaggedNodes.begin(),
66 NodeSUnit->FlaggedNodes.end());
Evan Chenge165a782006-05-11 23:55:42 +000067 }
68
69 // Scan down, adding this node and any flagged succs to FlaggedNodes if they
70 // have a user of the flag operand.
71 N = NI;
72 while (N->getValueType(N->getNumValues()-1) == MVT::Flag) {
73 SDOperand FlagVal(N, N->getNumValues()-1);
74
75 // There are either zero or one users of the Flag result.
76 bool HasFlagUse = false;
77 for (SDNode::use_iterator UI = N->use_begin(), E = N->use_end();
78 UI != E; ++UI)
79 if (FlagVal.isOperand(*UI)) {
80 HasFlagUse = true;
81 NodeSUnit->FlaggedNodes.push_back(N);
82 SUnitMap[N] = NodeSUnit;
83 N = *UI;
84 break;
85 }
Chris Lattner228a18e2006-08-17 00:09:56 +000086 if (!HasFlagUse) break;
Evan Chenge165a782006-05-11 23:55:42 +000087 }
88
89 // Now all flagged nodes are in FlaggedNodes and N is the bottom-most node.
90 // Update the SUnit
91 NodeSUnit->Node = N;
92 SUnitMap[N] = NodeSUnit;
93
94 // Compute the latency for the node. We use the sum of the latencies for
95 // all nodes flagged together into this SUnit.
96 if (InstrItins.isEmpty()) {
97 // No latency information.
98 NodeSUnit->Latency = 1;
99 } else {
100 NodeSUnit->Latency = 0;
101 if (N->isTargetOpcode()) {
102 unsigned SchedClass = TII->getSchedClass(N->getTargetOpcode());
103 InstrStage *S = InstrItins.begin(SchedClass);
104 InstrStage *E = InstrItins.end(SchedClass);
105 for (; S != E; ++S)
106 NodeSUnit->Latency += S->Cycles;
107 }
108 for (unsigned i = 0, e = NodeSUnit->FlaggedNodes.size(); i != e; ++i) {
109 SDNode *FNode = NodeSUnit->FlaggedNodes[i];
110 if (FNode->isTargetOpcode()) {
111 unsigned SchedClass = TII->getSchedClass(FNode->getTargetOpcode());
112 InstrStage *S = InstrItins.begin(SchedClass);
113 InstrStage *E = InstrItins.end(SchedClass);
114 for (; S != E; ++S)
115 NodeSUnit->Latency += S->Cycles;
116 }
117 }
118 }
119 }
120
121 // Pass 2: add the preds, succs, etc.
122 for (unsigned su = 0, e = SUnits.size(); su != e; ++su) {
123 SUnit *SU = &SUnits[su];
124 SDNode *MainNode = SU->Node;
125
126 if (MainNode->isTargetOpcode()) {
127 unsigned Opc = MainNode->getTargetOpcode();
Evan Cheng95f6ede2006-11-04 09:44:31 +0000128 for (unsigned i = 0, ee = TII->getNumOperands(Opc); i != ee; ++i) {
Evan Chengba59a1e2006-12-01 21:52:58 +0000129 if (TII->getOperandConstraint(Opc, i, TOI::TIED_TO) != -1) {
Evan Cheng95f6ede2006-11-04 09:44:31 +0000130 SU->isTwoAddress = true;
131 break;
132 }
133 }
Evan Cheng13d41b92006-05-12 01:58:24 +0000134 if (TII->isCommutableInstr(Opc))
135 SU->isCommutable = true;
Evan Chenge165a782006-05-11 23:55:42 +0000136 }
137
138 // Find all predecessors and successors of the group.
139 // Temporarily add N to make code simpler.
140 SU->FlaggedNodes.push_back(MainNode);
141
142 for (unsigned n = 0, e = SU->FlaggedNodes.size(); n != e; ++n) {
143 SDNode *N = SU->FlaggedNodes[n];
144
145 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
146 SDNode *OpN = N->getOperand(i).Val;
147 if (isPassiveNode(OpN)) continue; // Not scheduled.
148 SUnit *OpSU = SUnitMap[OpN];
149 assert(OpSU && "Node has no SUnit!");
150 if (OpSU == SU) continue; // In the same group.
151
152 MVT::ValueType OpVT = N->getOperand(i).getValueType();
153 assert(OpVT != MVT::Flag && "Flagged nodes should be in same sunit!");
154 bool isChain = OpVT == MVT::Other;
155
Chris Lattner228a18e2006-08-17 00:09:56 +0000156 if (SU->addPred(OpSU, isChain)) {
Evan Chenge165a782006-05-11 23:55:42 +0000157 if (!isChain) {
158 SU->NumPreds++;
159 SU->NumPredsLeft++;
160 } else {
161 SU->NumChainPredsLeft++;
162 }
163 }
Chris Lattner228a18e2006-08-17 00:09:56 +0000164 if (OpSU->addSucc(SU, isChain)) {
Evan Chenge165a782006-05-11 23:55:42 +0000165 if (!isChain) {
166 OpSU->NumSuccs++;
167 OpSU->NumSuccsLeft++;
168 } else {
169 OpSU->NumChainSuccsLeft++;
170 }
171 }
172 }
173 }
174
175 // Remove MainNode from FlaggedNodes again.
176 SU->FlaggedNodes.pop_back();
177 }
178
179 return;
180}
181
Chris Lattner228a18e2006-08-17 00:09:56 +0000182static void CalculateDepths(SUnit &SU, unsigned Depth) {
183 if (SU.Depth == 0 || Depth > SU.Depth) {
184 SU.Depth = Depth;
185 for (SUnit::succ_iterator I = SU.Succs.begin(), E = SU.Succs.end();
186 I != E; ++I)
187 CalculateDepths(*I->first, Depth+1);
Evan Cheng626da3d2006-05-12 06:05:18 +0000188 }
Evan Chenge165a782006-05-11 23:55:42 +0000189}
190
191void ScheduleDAG::CalculateDepths() {
192 SUnit *Entry = SUnitMap[DAG.getEntryNode().Val];
Chris Lattner228a18e2006-08-17 00:09:56 +0000193 ::CalculateDepths(*Entry, 0U);
Evan Chenge165a782006-05-11 23:55:42 +0000194 for (unsigned i = 0, e = SUnits.size(); i != e; ++i)
195 if (SUnits[i].Preds.size() == 0 && &SUnits[i] != Entry) {
Chris Lattner228a18e2006-08-17 00:09:56 +0000196 ::CalculateDepths(SUnits[i], 0U);
Evan Chenge165a782006-05-11 23:55:42 +0000197 }
198}
199
Chris Lattner228a18e2006-08-17 00:09:56 +0000200static void CalculateHeights(SUnit &SU, unsigned Height) {
201 if (SU.Height == 0 || Height > SU.Height) {
202 SU.Height = Height;
203 for (SUnit::pred_iterator I = SU.Preds.begin(), E = SU.Preds.end();
204 I != E; ++I)
205 CalculateHeights(*I->first, Height+1);
Evan Cheng626da3d2006-05-12 06:05:18 +0000206 }
Evan Chenge165a782006-05-11 23:55:42 +0000207}
208void ScheduleDAG::CalculateHeights() {
209 SUnit *Root = SUnitMap[DAG.getRoot().Val];
Chris Lattner228a18e2006-08-17 00:09:56 +0000210 ::CalculateHeights(*Root, 0U);
Evan Chenge165a782006-05-11 23:55:42 +0000211}
212
Jim Laskeye6b90fb2005-09-26 21:57:04 +0000213/// CountResults - The results of target nodes have register or immediate
214/// operands first, then an optional chain, and optional flag operands (which do
215/// not go into the machine instrs.)
Evan Cheng95f6ede2006-11-04 09:44:31 +0000216unsigned ScheduleDAG::CountResults(SDNode *Node) {
Jim Laskeyb6d4c2c2005-09-30 19:15:27 +0000217 unsigned N = Node->getNumValues();
218 while (N && Node->getValueType(N - 1) == MVT::Flag)
Jim Laskeye6b90fb2005-09-26 21:57:04 +0000219 --N;
Jim Laskeyb6d4c2c2005-09-30 19:15:27 +0000220 if (N && Node->getValueType(N - 1) == MVT::Other)
Jim Laskeye6b90fb2005-09-26 21:57:04 +0000221 --N; // Skip over chain result.
222 return N;
223}
224
225/// CountOperands The inputs to target nodes have any actual inputs first,
226/// followed by an optional chain operand, then flag operands. Compute the
227/// number of actual operands that will go into the machine instr.
Evan Cheng95f6ede2006-11-04 09:44:31 +0000228unsigned ScheduleDAG::CountOperands(SDNode *Node) {
Jim Laskeyb6d4c2c2005-09-30 19:15:27 +0000229 unsigned N = Node->getNumOperands();
230 while (N && Node->getOperand(N - 1).getValueType() == MVT::Flag)
Jim Laskeye6b90fb2005-09-26 21:57:04 +0000231 --N;
Jim Laskeyb6d4c2c2005-09-30 19:15:27 +0000232 if (N && Node->getOperand(N - 1).getValueType() == MVT::Other)
Jim Laskeye6b90fb2005-09-26 21:57:04 +0000233 --N; // Ignore chain if it exists.
234 return N;
235}
236
Jim Laskey60f09922006-07-21 20:57:35 +0000237static const TargetRegisterClass *getInstrOperandRegClass(
238 const MRegisterInfo *MRI,
239 const TargetInstrInfo *TII,
240 const TargetInstrDescriptor *II,
241 unsigned Op) {
242 if (Op >= II->numOperands) {
243 assert((II->Flags & M_VARIABLE_OPS)&& "Invalid operand # of instruction");
244 return NULL;
245 }
246 const TargetOperandInfo &toi = II->OpInfo[Op];
247 return (toi.Flags & M_LOOK_UP_PTR_REG_CLASS)
248 ? TII->getPointerRegClass() : MRI->getRegClass(toi.RegClass);
249}
250
251static unsigned CreateVirtualRegisters(const MRegisterInfo *MRI,
252 MachineInstr *MI,
Evan Cheng4ef10862006-01-23 07:01:07 +0000253 unsigned NumResults,
254 SSARegMap *RegMap,
Evan Cheng21d03f22006-05-18 20:42:07 +0000255 const TargetInstrInfo *TII,
Evan Cheng4ef10862006-01-23 07:01:07 +0000256 const TargetInstrDescriptor &II) {
Jim Laskeye6b90fb2005-09-26 21:57:04 +0000257 // Create the result registers for this node and add the result regs to
258 // the machine instruction.
Evan Cheng21d03f22006-05-18 20:42:07 +0000259 unsigned ResultReg =
Jim Laskey60f09922006-07-21 20:57:35 +0000260 RegMap->createVirtualRegister(getInstrOperandRegClass(MRI, TII, &II, 0));
Chris Lattner09e46062006-09-05 02:31:13 +0000261 MI->addRegOperand(ResultReg, true);
Jim Laskeye6b90fb2005-09-26 21:57:04 +0000262 for (unsigned i = 1; i != NumResults; ++i) {
Jim Laskey60f09922006-07-21 20:57:35 +0000263 const TargetRegisterClass *RC = getInstrOperandRegClass(MRI, TII, &II, i);
Evan Cheng21d03f22006-05-18 20:42:07 +0000264 assert(RC && "Isn't a register operand!");
Chris Lattner09e46062006-09-05 02:31:13 +0000265 MI->addRegOperand(RegMap->createVirtualRegister(RC), true);
Jim Laskeye6b90fb2005-09-26 21:57:04 +0000266 }
267 return ResultReg;
268}
269
Chris Lattnerdf375062006-03-10 07:25:12 +0000270/// getVR - Return the virtual register corresponding to the specified result
271/// of the specified node.
Chris Lattner831e0372007-02-04 08:47:20 +0000272static unsigned getVR(SDOperand Op, DenseMap<SDNode*, unsigned> &VRBaseMap) {
273 DenseMap<SDNode*, unsigned>::iterator I = VRBaseMap.find(Op.Val);
Chris Lattnerdf375062006-03-10 07:25:12 +0000274 assert(I != VRBaseMap.end() && "Node emitted out of order - late");
275 return I->second + Op.ResNo;
276}
277
278
Chris Lattnered18b682006-02-24 18:54:03 +0000279/// AddOperand - Add the specified operand to the specified machine instr. II
280/// specifies the instruction information for the node, and IIOpNum is the
281/// operand number (in the II) that we are adding. IIOpNum and II are used for
282/// assertions only.
283void ScheduleDAG::AddOperand(MachineInstr *MI, SDOperand Op,
284 unsigned IIOpNum,
Chris Lattnerdf375062006-03-10 07:25:12 +0000285 const TargetInstrDescriptor *II,
Chris Lattner831e0372007-02-04 08:47:20 +0000286 DenseMap<SDNode*, unsigned> &VRBaseMap) {
Chris Lattnered18b682006-02-24 18:54:03 +0000287 if (Op.isTargetOpcode()) {
288 // Note that this case is redundant with the final else block, but we
289 // include it because it is the most common and it makes the logic
290 // simpler here.
291 assert(Op.getValueType() != MVT::Other &&
292 Op.getValueType() != MVT::Flag &&
293 "Chain and flag operands should occur at end of operand list!");
294
295 // Get/emit the operand.
Chris Lattnerdf375062006-03-10 07:25:12 +0000296 unsigned VReg = getVR(Op, VRBaseMap);
Chris Lattner09e46062006-09-05 02:31:13 +0000297 MI->addRegOperand(VReg, false);
Chris Lattnered18b682006-02-24 18:54:03 +0000298
299 // Verify that it is right.
300 assert(MRegisterInfo::isVirtualRegister(VReg) && "Not a vreg?");
301 if (II) {
Jim Laskey60f09922006-07-21 20:57:35 +0000302 const TargetRegisterClass *RC =
303 getInstrOperandRegClass(MRI, TII, II, IIOpNum);
Evan Cheng21d03f22006-05-18 20:42:07 +0000304 assert(RC && "Don't have operand info for this instruction!");
Chris Lattner01528292007-02-15 18:17:56 +0000305 const TargetRegisterClass *VRC = RegMap->getRegClass(VReg);
306 if (VRC != RC) {
307 cerr << "Register class of operand and regclass of use don't agree!\n";
308#ifndef NDEBUG
309 cerr << "Operand = " << IIOpNum << "\n";
Chris Lattner95ad9432007-02-17 06:38:37 +0000310 cerr << "Op->Val = "; Op.Val->dump(&DAG); cerr << "\n";
Chris Lattner01528292007-02-15 18:17:56 +0000311 cerr << "MI = "; MI->print(cerr);
312 cerr << "VReg = " << VReg << "\n";
313 cerr << "VReg RegClass size = " << VRC->getSize()
Chris Lattner5d4a9f72007-02-15 18:19:15 +0000314 << ", align = " << VRC->getAlignment() << "\n";
Chris Lattner01528292007-02-15 18:17:56 +0000315 cerr << "Expected RegClass size = " << RC->getSize()
Chris Lattner5d4a9f72007-02-15 18:19:15 +0000316 << ", align = " << RC->getAlignment() << "\n";
Chris Lattner01528292007-02-15 18:17:56 +0000317#endif
318 cerr << "Fatal error, aborting.\n";
319 abort();
320 }
Chris Lattnered18b682006-02-24 18:54:03 +0000321 }
322 } else if (ConstantSDNode *C =
323 dyn_cast<ConstantSDNode>(Op)) {
Chris Lattner2d90ac72006-05-04 18:05:43 +0000324 MI->addImmOperand(C->getValue());
Evan Cheng489a87c2007-01-05 20:59:06 +0000325 } else if (RegisterSDNode *R =
Chris Lattnered18b682006-02-24 18:54:03 +0000326 dyn_cast<RegisterSDNode>(Op)) {
Chris Lattner09e46062006-09-05 02:31:13 +0000327 MI->addRegOperand(R->getReg(), false);
Chris Lattnered18b682006-02-24 18:54:03 +0000328 } else if (GlobalAddressSDNode *TGA =
329 dyn_cast<GlobalAddressSDNode>(Op)) {
Chris Lattnerea50fab2006-05-04 01:15:02 +0000330 MI->addGlobalAddressOperand(TGA->getGlobal(), TGA->getOffset());
Chris Lattnered18b682006-02-24 18:54:03 +0000331 } else if (BasicBlockSDNode *BB =
332 dyn_cast<BasicBlockSDNode>(Op)) {
333 MI->addMachineBasicBlockOperand(BB->getBasicBlock());
334 } else if (FrameIndexSDNode *FI =
335 dyn_cast<FrameIndexSDNode>(Op)) {
336 MI->addFrameIndexOperand(FI->getIndex());
Nate Begeman37efe672006-04-22 18:53:45 +0000337 } else if (JumpTableSDNode *JT =
338 dyn_cast<JumpTableSDNode>(Op)) {
339 MI->addJumpTableIndexOperand(JT->getIndex());
Chris Lattnered18b682006-02-24 18:54:03 +0000340 } else if (ConstantPoolSDNode *CP =
341 dyn_cast<ConstantPoolSDNode>(Op)) {
Evan Cheng404cb4f2006-02-25 09:54:52 +0000342 int Offset = CP->getOffset();
Chris Lattnered18b682006-02-24 18:54:03 +0000343 unsigned Align = CP->getAlignment();
Evan Chengd6594ae2006-09-12 21:00:35 +0000344 const Type *Type = CP->getType();
Chris Lattnered18b682006-02-24 18:54:03 +0000345 // MachineConstantPool wants an explicit alignment.
346 if (Align == 0) {
Evan Chengde268f72007-01-24 07:03:39 +0000347 Align = TM.getTargetData()->getPreferredTypeAlignmentShift(Type);
Evan Chengf6d039a2007-01-22 23:13:55 +0000348 if (Align == 0) {
Reid Spencerac9dcb92007-02-15 03:39:18 +0000349 // Alignment of vector types. FIXME!
Evan Chengf6d039a2007-01-22 23:13:55 +0000350 Align = TM.getTargetData()->getTypeSize(Type);
351 Align = Log2_64(Align);
Chris Lattner54a30b92006-03-20 01:51:46 +0000352 }
Chris Lattnered18b682006-02-24 18:54:03 +0000353 }
354
Evan Chengd6594ae2006-09-12 21:00:35 +0000355 unsigned Idx;
356 if (CP->isMachineConstantPoolEntry())
357 Idx = ConstPool->getConstantPoolIndex(CP->getMachineCPVal(), Align);
358 else
359 Idx = ConstPool->getConstantPoolIndex(CP->getConstVal(), Align);
Evan Cheng404cb4f2006-02-25 09:54:52 +0000360 MI->addConstantPoolIndexOperand(Idx, Offset);
Chris Lattnered18b682006-02-24 18:54:03 +0000361 } else if (ExternalSymbolSDNode *ES =
362 dyn_cast<ExternalSymbolSDNode>(Op)) {
Chris Lattnerea50fab2006-05-04 01:15:02 +0000363 MI->addExternalSymbolOperand(ES->getSymbol());
Chris Lattnered18b682006-02-24 18:54:03 +0000364 } else {
365 assert(Op.getValueType() != MVT::Other &&
366 Op.getValueType() != MVT::Flag &&
367 "Chain and flag operands should occur at end of operand list!");
Chris Lattnerdf375062006-03-10 07:25:12 +0000368 unsigned VReg = getVR(Op, VRBaseMap);
Chris Lattner09e46062006-09-05 02:31:13 +0000369 MI->addRegOperand(VReg, false);
Chris Lattnered18b682006-02-24 18:54:03 +0000370
371 // Verify that it is right.
372 assert(MRegisterInfo::isVirtualRegister(VReg) && "Not a vreg?");
373 if (II) {
Jim Laskey60f09922006-07-21 20:57:35 +0000374 const TargetRegisterClass *RC =
375 getInstrOperandRegClass(MRI, TII, II, IIOpNum);
Evan Cheng21d03f22006-05-18 20:42:07 +0000376 assert(RC && "Don't have operand info for this instruction!");
377 assert(RegMap->getRegClass(VReg) == RC &&
Chris Lattnered18b682006-02-24 18:54:03 +0000378 "Register class of operand and regclass of use don't agree!");
379 }
380 }
381
382}
383
384
Jim Laskeyb6d4c2c2005-09-30 19:15:27 +0000385/// EmitNode - Generate machine code for an node and needed dependencies.
Jim Laskeye6b90fb2005-09-26 21:57:04 +0000386///
Chris Lattner8c7ef052006-03-10 07:28:36 +0000387void ScheduleDAG::EmitNode(SDNode *Node,
Chris Lattner831e0372007-02-04 08:47:20 +0000388 DenseMap<SDNode*, unsigned> &VRBaseMap) {
Jim Laskeyb6d4c2c2005-09-30 19:15:27 +0000389 unsigned VRBase = 0; // First virtual register for node
Chris Lattner2d973e42005-08-18 20:07:59 +0000390
Jim Laskeyb6d4c2c2005-09-30 19:15:27 +0000391 // If machine instruction
392 if (Node->isTargetOpcode()) {
393 unsigned Opc = Node->getTargetOpcode();
Evan Chenga9c20912006-01-21 02:32:06 +0000394 const TargetInstrDescriptor &II = TII->get(Opc);
Chris Lattner2d973e42005-08-18 20:07:59 +0000395
Jim Laskeyb6d4c2c2005-09-30 19:15:27 +0000396 unsigned NumResults = CountResults(Node);
397 unsigned NodeOperands = CountOperands(Node);
Jim Laskeye6b90fb2005-09-26 21:57:04 +0000398 unsigned NumMIOperands = NodeOperands + NumResults;
Chris Lattnerda8abb02005-09-01 18:44:10 +0000399#ifndef NDEBUG
Evan Cheng8d3af5e2006-06-15 07:22:16 +0000400 assert((unsigned(II.numOperands) == NumMIOperands ||
401 (II.Flags & M_VARIABLE_OPS)) &&
Chris Lattner2d973e42005-08-18 20:07:59 +0000402 "#operands for dag node doesn't match .td file!");
Chris Lattnerca6aa2f2005-08-19 01:01:34 +0000403#endif
Chris Lattner2d973e42005-08-18 20:07:59 +0000404
405 // Create the new machine instruction.
Evan Chengc0f64ff2006-11-27 23:37:22 +0000406 MachineInstr *MI = new MachineInstr(II);
Chris Lattner2d973e42005-08-18 20:07:59 +0000407
408 // Add result register values for things that are defined by this
409 // instruction.
Chris Lattnera4176522005-10-30 18:54:27 +0000410
411 // If the node is only used by a CopyToReg and the dest reg is a vreg, use
412 // the CopyToReg'd destination register instead of creating a new vreg.
413 if (NumResults == 1) {
414 for (SDNode::use_iterator UI = Node->use_begin(), E = Node->use_end();
415 UI != E; ++UI) {
416 SDNode *Use = *UI;
417 if (Use->getOpcode() == ISD::CopyToReg &&
418 Use->getOperand(2).Val == Node) {
419 unsigned Reg = cast<RegisterSDNode>(Use->getOperand(1))->getReg();
420 if (MRegisterInfo::isVirtualRegister(Reg)) {
421 VRBase = Reg;
Chris Lattner09e46062006-09-05 02:31:13 +0000422 MI->addRegOperand(Reg, true);
Chris Lattnera4176522005-10-30 18:54:27 +0000423 break;
424 }
425 }
426 }
427 }
428
429 // Otherwise, create new virtual registers.
430 if (NumResults && VRBase == 0)
Jim Laskey60f09922006-07-21 20:57:35 +0000431 VRBase = CreateVirtualRegisters(MRI, MI, NumResults, RegMap, TII, II);
Jim Laskeyb6d4c2c2005-09-30 19:15:27 +0000432
433 // Emit all of the actual operands of this instruction, adding them to the
434 // instruction as appropriate.
Chris Lattnered18b682006-02-24 18:54:03 +0000435 for (unsigned i = 0; i != NodeOperands; ++i)
Chris Lattnerdf375062006-03-10 07:25:12 +0000436 AddOperand(MI, Node->getOperand(i), i+NumResults, &II, VRBaseMap);
Evan Cheng13d41b92006-05-12 01:58:24 +0000437
438 // Commute node if it has been determined to be profitable.
439 if (CommuteSet.count(Node)) {
440 MachineInstr *NewMI = TII->commuteInstruction(MI);
441 if (NewMI == 0)
Bill Wendling832171c2006-12-07 20:04:42 +0000442 DOUT << "Sched: COMMUTING FAILED!\n";
Evan Cheng13d41b92006-05-12 01:58:24 +0000443 else {
Bill Wendling832171c2006-12-07 20:04:42 +0000444 DOUT << "Sched: COMMUTED TO: " << *NewMI;
Evan Cheng4c6f2f92006-05-31 18:03:39 +0000445 if (MI != NewMI) {
446 delete MI;
447 MI = NewMI;
448 }
Evan Cheng13d41b92006-05-12 01:58:24 +0000449 }
450 }
451
Jim Laskeyb6d4c2c2005-09-30 19:15:27 +0000452 // Now that we have emitted all operands, emit this instruction itself.
453 if ((II.Flags & M_USES_CUSTOM_DAG_SCHED_INSERTION) == 0) {
454 BB->insert(BB->end(), MI);
455 } else {
456 // Insert this instruction into the end of the basic block, potentially
457 // taking some custom action.
458 BB = DAG.getTargetLoweringInfo().InsertAtEndOfBasicBlock(MI, BB);
459 }
460 } else {
461 switch (Node->getOpcode()) {
462 default:
Jim Laskey16d42c62006-07-11 18:25:13 +0000463#ifndef NDEBUG
464 Node->dump();
465#endif
Jim Laskeyb6d4c2c2005-09-30 19:15:27 +0000466 assert(0 && "This target-independent node should have been selected!");
467 case ISD::EntryToken: // fall thru
468 case ISD::TokenFactor:
Jim Laskey1ee29252007-01-26 14:34:52 +0000469 case ISD::LABEL:
Jim Laskeyb6d4c2c2005-09-30 19:15:27 +0000470 break;
471 case ISD::CopyToReg: {
Evan Cheng489a87c2007-01-05 20:59:06 +0000472 unsigned InReg;
473 if (RegisterSDNode *R = dyn_cast<RegisterSDNode>(Node->getOperand(2)))
474 InReg = R->getReg();
475 else
476 InReg = getVR(Node->getOperand(2), VRBaseMap);
Chris Lattnera4176522005-10-30 18:54:27 +0000477 unsigned DestReg = cast<RegisterSDNode>(Node->getOperand(1))->getReg();
Chris Lattner45053fc2006-03-24 07:15:07 +0000478 if (InReg != DestReg) // Coalesced away the copy?
Evan Chenga9c20912006-01-21 02:32:06 +0000479 MRI->copyRegToReg(*BB, BB->end(), DestReg, InReg,
480 RegMap->getRegClass(InReg));
Jim Laskeyb6d4c2c2005-09-30 19:15:27 +0000481 break;
482 }
483 case ISD::CopyFromReg: {
484 unsigned SrcReg = cast<RegisterSDNode>(Node->getOperand(1))->getReg();
Chris Lattner089c25c2005-10-09 05:58:56 +0000485 if (MRegisterInfo::isVirtualRegister(SrcReg)) {
486 VRBase = SrcReg; // Just use the input register directly!
487 break;
488 }
489
Chris Lattnera4176522005-10-30 18:54:27 +0000490 // If the node is only used by a CopyToReg and the dest reg is a vreg, use
491 // the CopyToReg'd destination register instead of creating a new vreg.
492 for (SDNode::use_iterator UI = Node->use_begin(), E = Node->use_end();
493 UI != E; ++UI) {
494 SDNode *Use = *UI;
495 if (Use->getOpcode() == ISD::CopyToReg &&
496 Use->getOperand(2).Val == Node) {
497 unsigned DestReg = cast<RegisterSDNode>(Use->getOperand(1))->getReg();
498 if (MRegisterInfo::isVirtualRegister(DestReg)) {
499 VRBase = DestReg;
500 break;
501 }
502 }
503 }
504
Jim Laskeyb6d4c2c2005-09-30 19:15:27 +0000505 // Figure out the register class to create for the destreg.
506 const TargetRegisterClass *TRC = 0;
Chris Lattnera4176522005-10-30 18:54:27 +0000507 if (VRBase) {
508 TRC = RegMap->getRegClass(VRBase);
509 } else {
Chris Lattner089c25c2005-10-09 05:58:56 +0000510
Chris Lattnera4176522005-10-30 18:54:27 +0000511 // Pick the register class of the right type that contains this physreg.
Evan Chenga9c20912006-01-21 02:32:06 +0000512 for (MRegisterInfo::regclass_iterator I = MRI->regclass_begin(),
513 E = MRI->regclass_end(); I != E; ++I)
Nate Begeman6510b222005-12-01 04:51:06 +0000514 if ((*I)->hasType(Node->getValueType(0)) &&
Chris Lattnera4176522005-10-30 18:54:27 +0000515 (*I)->contains(SrcReg)) {
516 TRC = *I;
517 break;
518 }
519 assert(TRC && "Couldn't find register class for reg copy!");
Jim Laskeyb6d4c2c2005-09-30 19:15:27 +0000520
Chris Lattnera4176522005-10-30 18:54:27 +0000521 // Create the reg, emit the copy.
522 VRBase = RegMap->createVirtualRegister(TRC);
523 }
Evan Chenga9c20912006-01-21 02:32:06 +0000524 MRI->copyRegToReg(*BB, BB->end(), VRBase, SrcReg, TRC);
Jim Laskeyb6d4c2c2005-09-30 19:15:27 +0000525 break;
526 }
Chris Lattneracc43bf2006-01-26 23:28:04 +0000527 case ISD::INLINEASM: {
528 unsigned NumOps = Node->getNumOperands();
529 if (Node->getOperand(NumOps-1).getValueType() == MVT::Flag)
530 --NumOps; // Ignore the flag operand.
531
532 // Create the inline asm machine instruction.
533 MachineInstr *MI =
Evan Chengc0f64ff2006-11-27 23:37:22 +0000534 new MachineInstr(BB, TII->get(TargetInstrInfo::INLINEASM));
Chris Lattneracc43bf2006-01-26 23:28:04 +0000535
536 // Add the asm string as an external symbol operand.
537 const char *AsmStr =
538 cast<ExternalSymbolSDNode>(Node->getOperand(1))->getSymbol();
Chris Lattnerea50fab2006-05-04 01:15:02 +0000539 MI->addExternalSymbolOperand(AsmStr);
Chris Lattneracc43bf2006-01-26 23:28:04 +0000540
541 // Add all of the operand registers to the instruction.
Chris Lattnerc3a9f8d2006-02-23 19:21:04 +0000542 for (unsigned i = 2; i != NumOps;) {
543 unsigned Flags = cast<ConstantSDNode>(Node->getOperand(i))->getValue();
Chris Lattnerfd6d2822006-02-24 19:18:20 +0000544 unsigned NumVals = Flags >> 3;
Chris Lattnerc3a9f8d2006-02-23 19:21:04 +0000545
Chris Lattner2d90ac72006-05-04 18:05:43 +0000546 MI->addImmOperand(Flags);
Chris Lattnerc3a9f8d2006-02-23 19:21:04 +0000547 ++i; // Skip the ID value.
548
549 switch (Flags & 7) {
Chris Lattneracc43bf2006-01-26 23:28:04 +0000550 default: assert(0 && "Bad flags!");
Chris Lattnerc3a9f8d2006-02-23 19:21:04 +0000551 case 1: // Use of register.
Chris Lattnerfd6d2822006-02-24 19:18:20 +0000552 for (; NumVals; --NumVals, ++i) {
Chris Lattnerc3a9f8d2006-02-23 19:21:04 +0000553 unsigned Reg = cast<RegisterSDNode>(Node->getOperand(i))->getReg();
Chris Lattner09e46062006-09-05 02:31:13 +0000554 MI->addRegOperand(Reg, false);
Chris Lattnerc3a9f8d2006-02-23 19:21:04 +0000555 }
Chris Lattnerdc19b702006-02-04 02:26:14 +0000556 break;
Chris Lattnerc3a9f8d2006-02-23 19:21:04 +0000557 case 2: // Def of register.
Chris Lattnerfd6d2822006-02-24 19:18:20 +0000558 for (; NumVals; --NumVals, ++i) {
Chris Lattnerc3a9f8d2006-02-23 19:21:04 +0000559 unsigned Reg = cast<RegisterSDNode>(Node->getOperand(i))->getReg();
Chris Lattner09e46062006-09-05 02:31:13 +0000560 MI->addRegOperand(Reg, true);
Chris Lattnerc3a9f8d2006-02-23 19:21:04 +0000561 }
Chris Lattnerdc19b702006-02-04 02:26:14 +0000562 break;
Chris Lattnerdc19b702006-02-04 02:26:14 +0000563 case 3: { // Immediate.
Chris Lattnerfd6d2822006-02-24 19:18:20 +0000564 assert(NumVals == 1 && "Unknown immediate value!");
Chris Lattnerefa46ce2006-10-31 20:01:56 +0000565 if (ConstantSDNode *CS=dyn_cast<ConstantSDNode>(Node->getOperand(i))){
566 MI->addImmOperand(CS->getValue());
567 } else {
568 GlobalAddressSDNode *GA =
569 cast<GlobalAddressSDNode>(Node->getOperand(i));
570 MI->addGlobalAddressOperand(GA->getGlobal(), GA->getOffset());
571 }
Chris Lattnerc3a9f8d2006-02-23 19:21:04 +0000572 ++i;
Chris Lattnerdc19b702006-02-04 02:26:14 +0000573 break;
574 }
Chris Lattnerfd6d2822006-02-24 19:18:20 +0000575 case 4: // Addressing mode.
576 // The addressing mode has been selected, just add all of the
577 // operands to the machine instruction.
578 for (; NumVals; --NumVals, ++i)
Chris Lattnerdf375062006-03-10 07:25:12 +0000579 AddOperand(MI, Node->getOperand(i), 0, 0, VRBaseMap);
Chris Lattnerfd6d2822006-02-24 19:18:20 +0000580 break;
Chris Lattnerdc19b702006-02-04 02:26:14 +0000581 }
Chris Lattneracc43bf2006-01-26 23:28:04 +0000582 }
583 break;
584 }
Jim Laskeyb6d4c2c2005-09-30 19:15:27 +0000585 }
586 }
587
Chris Lattnerdf375062006-03-10 07:25:12 +0000588 assert(!VRBaseMap.count(Node) && "Node emitted out of order - early");
589 VRBaseMap[Node] = VRBase;
Jim Laskeyb6d4c2c2005-09-30 19:15:27 +0000590}
591
Chris Lattnera93dfcd2006-03-05 23:51:47 +0000592void ScheduleDAG::EmitNoop() {
593 TII->insertNoop(*BB, BB->end());
594}
595
Evan Chenge165a782006-05-11 23:55:42 +0000596/// EmitSchedule - Emit the machine code in scheduled order.
597void ScheduleDAG::EmitSchedule() {
Chris Lattner96645412006-05-16 06:10:58 +0000598 // If this is the first basic block in the function, and if it has live ins
599 // that need to be copied into vregs, emit the copies into the top of the
600 // block before emitting the code for the block.
601 MachineFunction &MF = DAG.getMachineFunction();
602 if (&MF.front() == BB && MF.livein_begin() != MF.livein_end()) {
603 for (MachineFunction::livein_iterator LI = MF.livein_begin(),
604 E = MF.livein_end(); LI != E; ++LI)
605 if (LI->second)
606 MRI->copyRegToReg(*MF.begin(), MF.begin()->end(), LI->second,
607 LI->first, RegMap->getRegClass(LI->second));
608 }
609
610
611 // Finally, emit the code for all of the scheduled instructions.
Chris Lattner831e0372007-02-04 08:47:20 +0000612 DenseMap<SDNode*, unsigned> VRBaseMap;
Evan Chenge165a782006-05-11 23:55:42 +0000613 for (unsigned i = 0, e = Sequence.size(); i != e; i++) {
614 if (SUnit *SU = Sequence[i]) {
615 for (unsigned j = 0, ee = SU->FlaggedNodes.size(); j != ee; j++)
616 EmitNode(SU->FlaggedNodes[j], VRBaseMap);
617 EmitNode(SU->Node, VRBaseMap);
618 } else {
619 // Null SUnit* is a noop.
620 EmitNoop();
621 }
622 }
623}
624
625/// dump - dump the schedule.
626void ScheduleDAG::dumpSchedule() const {
627 for (unsigned i = 0, e = Sequence.size(); i != e; i++) {
628 if (SUnit *SU = Sequence[i])
629 SU->dump(&DAG);
630 else
Bill Wendling832171c2006-12-07 20:04:42 +0000631 cerr << "**** NOOP ****\n";
Evan Chenge165a782006-05-11 23:55:42 +0000632 }
633}
634
635
Evan Chenga9c20912006-01-21 02:32:06 +0000636/// Run - perform scheduling.
637///
638MachineBasicBlock *ScheduleDAG::Run() {
639 TII = TM.getInstrInfo();
640 MRI = TM.getRegisterInfo();
641 RegMap = BB->getParent()->getSSARegMap();
642 ConstPool = BB->getParent()->getConstantPool();
Evan Cheng4ef10862006-01-23 07:01:07 +0000643
Evan Chenga9c20912006-01-21 02:32:06 +0000644 Schedule();
645 return BB;
Chris Lattnerd32b2362005-08-18 18:45:24 +0000646}
Evan Cheng4ef10862006-01-23 07:01:07 +0000647
Evan Chenge165a782006-05-11 23:55:42 +0000648/// SUnit - Scheduling unit. It's an wrapper around either a single SDNode or
649/// a group of nodes flagged together.
650void SUnit::dump(const SelectionDAG *G) const {
Bill Wendling832171c2006-12-07 20:04:42 +0000651 cerr << "SU(" << NodeNum << "): ";
Evan Chenge165a782006-05-11 23:55:42 +0000652 Node->dump(G);
Bill Wendling832171c2006-12-07 20:04:42 +0000653 cerr << "\n";
Evan Chenge165a782006-05-11 23:55:42 +0000654 if (FlaggedNodes.size() != 0) {
655 for (unsigned i = 0, e = FlaggedNodes.size(); i != e; i++) {
Bill Wendling832171c2006-12-07 20:04:42 +0000656 cerr << " ";
Evan Chenge165a782006-05-11 23:55:42 +0000657 FlaggedNodes[i]->dump(G);
Bill Wendling832171c2006-12-07 20:04:42 +0000658 cerr << "\n";
Evan Chenge165a782006-05-11 23:55:42 +0000659 }
660 }
661}
Evan Cheng4ef10862006-01-23 07:01:07 +0000662
Evan Chenge165a782006-05-11 23:55:42 +0000663void SUnit::dumpAll(const SelectionDAG *G) const {
664 dump(G);
665
Bill Wendling832171c2006-12-07 20:04:42 +0000666 cerr << " # preds left : " << NumPredsLeft << "\n";
667 cerr << " # succs left : " << NumSuccsLeft << "\n";
668 cerr << " # chain preds left : " << NumChainPredsLeft << "\n";
669 cerr << " # chain succs left : " << NumChainSuccsLeft << "\n";
670 cerr << " Latency : " << Latency << "\n";
671 cerr << " Depth : " << Depth << "\n";
672 cerr << " Height : " << Height << "\n";
Evan Chenge165a782006-05-11 23:55:42 +0000673
674 if (Preds.size() != 0) {
Bill Wendling832171c2006-12-07 20:04:42 +0000675 cerr << " Predecessors:\n";
Chris Lattner228a18e2006-08-17 00:09:56 +0000676 for (SUnit::const_succ_iterator I = Preds.begin(), E = Preds.end();
677 I != E; ++I) {
Evan Chenge165a782006-05-11 23:55:42 +0000678 if (I->second)
Bill Wendling832171c2006-12-07 20:04:42 +0000679 cerr << " ch #";
Evan Chenge165a782006-05-11 23:55:42 +0000680 else
Bill Wendling832171c2006-12-07 20:04:42 +0000681 cerr << " val #";
682 cerr << I->first << " - SU(" << I->first->NodeNum << ")\n";
Evan Chenge165a782006-05-11 23:55:42 +0000683 }
684 }
685 if (Succs.size() != 0) {
Bill Wendling832171c2006-12-07 20:04:42 +0000686 cerr << " Successors:\n";
Chris Lattner228a18e2006-08-17 00:09:56 +0000687 for (SUnit::const_succ_iterator I = Succs.begin(), E = Succs.end();
688 I != E; ++I) {
Evan Chenge165a782006-05-11 23:55:42 +0000689 if (I->second)
Bill Wendling832171c2006-12-07 20:04:42 +0000690 cerr << " ch #";
Evan Chenge165a782006-05-11 23:55:42 +0000691 else
Bill Wendling832171c2006-12-07 20:04:42 +0000692 cerr << " val #";
693 cerr << I->first << " - SU(" << I->first->NodeNum << ")\n";
Evan Chenge165a782006-05-11 23:55:42 +0000694 }
695 }
Bill Wendling832171c2006-12-07 20:04:42 +0000696 cerr << "\n";
Evan Chenge165a782006-05-11 23:55:42 +0000697}