blob: dd968ed4544b51a2336ab806f91d13ba0bedae6c [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"
Chris Lattnerb0d21ef2006-03-08 04:25:59 +000017#include "llvm/CodeGen/ScheduleDAG.h"
Chris Lattner5839bf22005-08-26 17:15:30 +000018#include "llvm/CodeGen/MachineConstantPool.h"
Chris Lattner4ccd4062005-08-19 20:45:43 +000019#include "llvm/CodeGen/MachineFunction.h"
Chris Lattner4ccd4062005-08-19 20:45:43 +000020#include "llvm/CodeGen/SSARegMap.h"
Owen Anderson07000c62006-05-12 06:33:49 +000021#include "llvm/Target/TargetData.h"
Chris Lattner2d973e42005-08-18 20:07:59 +000022#include "llvm/Target/TargetMachine.h"
23#include "llvm/Target/TargetInstrInfo.h"
Chris Lattner025c39b2005-08-26 20:54:47 +000024#include "llvm/Target/TargetLowering.h"
Evan Chenge165a782006-05-11 23:55:42 +000025#include "llvm/Support/Debug.h"
Chris Lattner54a30b92006-03-20 01:51:46 +000026#include "llvm/Support/MathExtras.h"
Evan Chenge165a782006-05-11 23:55:42 +000027#include <iostream>
Chris Lattnerd32b2362005-08-18 18:45:24 +000028using namespace llvm;
29
Jim Laskeye6b90fb2005-09-26 21:57:04 +000030
Evan Chenge165a782006-05-11 23:55:42 +000031/// BuildSchedUnits - Build SUnits from the selection dag that we are input.
32/// This SUnit graph is similar to the SelectionDAG, but represents flagged
33/// together nodes with a single SUnit.
34void ScheduleDAG::BuildSchedUnits() {
35 // Reserve entries in the vector for each of the SUnits we are creating. This
36 // ensure that reallocation of the vector won't happen, so SUnit*'s won't get
37 // invalidated.
38 SUnits.reserve(std::distance(DAG.allnodes_begin(), DAG.allnodes_end()));
39
40 const InstrItineraryData &InstrItins = TM.getInstrItineraryData();
41
42 for (SelectionDAG::allnodes_iterator NI = DAG.allnodes_begin(),
43 E = DAG.allnodes_end(); NI != E; ++NI) {
44 if (isPassiveNode(NI)) // Leaf node, e.g. a TargetImmediate.
45 continue;
46
47 // If this node has already been processed, stop now.
48 if (SUnitMap[NI]) continue;
49
50 SUnit *NodeSUnit = NewSUnit(NI);
51
52 // See if anything is flagged to this node, if so, add them to flagged
53 // nodes. Nodes can have at most one flag input and one flag output. Flags
54 // are required the be the last operand and result of a node.
55
56 // Scan up, adding flagged preds to FlaggedNodes.
57 SDNode *N = NI;
Evan Cheng3b97acd2006-08-07 22:12:12 +000058 if (N->getNumOperands() &&
59 N->getOperand(N->getNumOperands()-1).getValueType() == MVT::Flag) {
60 do {
61 N = N->getOperand(N->getNumOperands()-1).Val;
62 NodeSUnit->FlaggedNodes.push_back(N);
63 SUnitMap[N] = NodeSUnit;
64 } while (N->getNumOperands() &&
65 N->getOperand(N->getNumOperands()-1).getValueType()== MVT::Flag);
66 std::reverse(NodeSUnit->FlaggedNodes.begin(),
67 NodeSUnit->FlaggedNodes.end());
Evan Chenge165a782006-05-11 23:55:42 +000068 }
69
70 // Scan down, adding this node and any flagged succs to FlaggedNodes if they
71 // have a user of the flag operand.
72 N = NI;
73 while (N->getValueType(N->getNumValues()-1) == MVT::Flag) {
74 SDOperand FlagVal(N, N->getNumValues()-1);
75
76 // There are either zero or one users of the Flag result.
77 bool HasFlagUse = false;
78 for (SDNode::use_iterator UI = N->use_begin(), E = N->use_end();
79 UI != E; ++UI)
80 if (FlagVal.isOperand(*UI)) {
81 HasFlagUse = true;
82 NodeSUnit->FlaggedNodes.push_back(N);
83 SUnitMap[N] = NodeSUnit;
84 N = *UI;
85 break;
86 }
Chris Lattner228a18e2006-08-17 00:09:56 +000087 if (!HasFlagUse) break;
Evan Chenge165a782006-05-11 23:55:42 +000088 }
89
90 // Now all flagged nodes are in FlaggedNodes and N is the bottom-most node.
91 // Update the SUnit
92 NodeSUnit->Node = N;
93 SUnitMap[N] = NodeSUnit;
94
95 // Compute the latency for the node. We use the sum of the latencies for
96 // all nodes flagged together into this SUnit.
97 if (InstrItins.isEmpty()) {
98 // No latency information.
99 NodeSUnit->Latency = 1;
100 } else {
101 NodeSUnit->Latency = 0;
102 if (N->isTargetOpcode()) {
103 unsigned SchedClass = TII->getSchedClass(N->getTargetOpcode());
104 InstrStage *S = InstrItins.begin(SchedClass);
105 InstrStage *E = InstrItins.end(SchedClass);
106 for (; S != E; ++S)
107 NodeSUnit->Latency += S->Cycles;
108 }
109 for (unsigned i = 0, e = NodeSUnit->FlaggedNodes.size(); i != e; ++i) {
110 SDNode *FNode = NodeSUnit->FlaggedNodes[i];
111 if (FNode->isTargetOpcode()) {
112 unsigned SchedClass = TII->getSchedClass(FNode->getTargetOpcode());
113 InstrStage *S = InstrItins.begin(SchedClass);
114 InstrStage *E = InstrItins.end(SchedClass);
115 for (; S != E; ++S)
116 NodeSUnit->Latency += S->Cycles;
117 }
118 }
119 }
120 }
121
122 // Pass 2: add the preds, succs, etc.
123 for (unsigned su = 0, e = SUnits.size(); su != e; ++su) {
124 SUnit *SU = &SUnits[su];
125 SDNode *MainNode = SU->Node;
126
127 if (MainNode->isTargetOpcode()) {
128 unsigned Opc = MainNode->getTargetOpcode();
Evan Cheng95f6ede2006-11-04 09:44:31 +0000129 for (unsigned i = 0, ee = TII->getNumOperands(Opc); i != ee; ++i) {
130 if (TII->getOperandConstraint(Opc, i,
131 TargetInstrInfo::TIED_TO) != -1) {
132 SU->isTwoAddress = true;
133 break;
134 }
135 }
Evan Cheng13d41b92006-05-12 01:58:24 +0000136 if (TII->isCommutableInstr(Opc))
137 SU->isCommutable = true;
Evan Chenge165a782006-05-11 23:55:42 +0000138 }
139
140 // Find all predecessors and successors of the group.
141 // Temporarily add N to make code simpler.
142 SU->FlaggedNodes.push_back(MainNode);
143
144 for (unsigned n = 0, e = SU->FlaggedNodes.size(); n != e; ++n) {
145 SDNode *N = SU->FlaggedNodes[n];
146
147 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
148 SDNode *OpN = N->getOperand(i).Val;
149 if (isPassiveNode(OpN)) continue; // Not scheduled.
150 SUnit *OpSU = SUnitMap[OpN];
151 assert(OpSU && "Node has no SUnit!");
152 if (OpSU == SU) continue; // In the same group.
153
154 MVT::ValueType OpVT = N->getOperand(i).getValueType();
155 assert(OpVT != MVT::Flag && "Flagged nodes should be in same sunit!");
156 bool isChain = OpVT == MVT::Other;
157
Chris Lattner228a18e2006-08-17 00:09:56 +0000158 if (SU->addPred(OpSU, isChain)) {
Evan Chenge165a782006-05-11 23:55:42 +0000159 if (!isChain) {
160 SU->NumPreds++;
161 SU->NumPredsLeft++;
162 } else {
163 SU->NumChainPredsLeft++;
164 }
165 }
Chris Lattner228a18e2006-08-17 00:09:56 +0000166 if (OpSU->addSucc(SU, isChain)) {
Evan Chenge165a782006-05-11 23:55:42 +0000167 if (!isChain) {
168 OpSU->NumSuccs++;
169 OpSU->NumSuccsLeft++;
170 } else {
171 OpSU->NumChainSuccsLeft++;
172 }
173 }
174 }
175 }
176
177 // Remove MainNode from FlaggedNodes again.
178 SU->FlaggedNodes.pop_back();
179 }
180
181 return;
182}
183
Chris Lattner228a18e2006-08-17 00:09:56 +0000184static void CalculateDepths(SUnit &SU, unsigned Depth) {
185 if (SU.Depth == 0 || Depth > SU.Depth) {
186 SU.Depth = Depth;
187 for (SUnit::succ_iterator I = SU.Succs.begin(), E = SU.Succs.end();
188 I != E; ++I)
189 CalculateDepths(*I->first, Depth+1);
Evan Cheng626da3d2006-05-12 06:05:18 +0000190 }
Evan Chenge165a782006-05-11 23:55:42 +0000191}
192
193void ScheduleDAG::CalculateDepths() {
194 SUnit *Entry = SUnitMap[DAG.getEntryNode().Val];
Chris Lattner228a18e2006-08-17 00:09:56 +0000195 ::CalculateDepths(*Entry, 0U);
Evan Chenge165a782006-05-11 23:55:42 +0000196 for (unsigned i = 0, e = SUnits.size(); i != e; ++i)
197 if (SUnits[i].Preds.size() == 0 && &SUnits[i] != Entry) {
Chris Lattner228a18e2006-08-17 00:09:56 +0000198 ::CalculateDepths(SUnits[i], 0U);
Evan Chenge165a782006-05-11 23:55:42 +0000199 }
200}
201
Chris Lattner228a18e2006-08-17 00:09:56 +0000202static void CalculateHeights(SUnit &SU, unsigned Height) {
203 if (SU.Height == 0 || Height > SU.Height) {
204 SU.Height = Height;
205 for (SUnit::pred_iterator I = SU.Preds.begin(), E = SU.Preds.end();
206 I != E; ++I)
207 CalculateHeights(*I->first, Height+1);
Evan Cheng626da3d2006-05-12 06:05:18 +0000208 }
Evan Chenge165a782006-05-11 23:55:42 +0000209}
210void ScheduleDAG::CalculateHeights() {
211 SUnit *Root = SUnitMap[DAG.getRoot().Val];
Chris Lattner228a18e2006-08-17 00:09:56 +0000212 ::CalculateHeights(*Root, 0U);
Evan Chenge165a782006-05-11 23:55:42 +0000213}
214
Jim Laskeye6b90fb2005-09-26 21:57:04 +0000215/// CountResults - The results of target nodes have register or immediate
216/// operands first, then an optional chain, and optional flag operands (which do
217/// not go into the machine instrs.)
Evan Cheng95f6ede2006-11-04 09:44:31 +0000218unsigned ScheduleDAG::CountResults(SDNode *Node) {
Jim Laskeyb6d4c2c2005-09-30 19:15:27 +0000219 unsigned N = Node->getNumValues();
220 while (N && Node->getValueType(N - 1) == MVT::Flag)
Jim Laskeye6b90fb2005-09-26 21:57:04 +0000221 --N;
Jim Laskeyb6d4c2c2005-09-30 19:15:27 +0000222 if (N && Node->getValueType(N - 1) == MVT::Other)
Jim Laskeye6b90fb2005-09-26 21:57:04 +0000223 --N; // Skip over chain result.
224 return N;
225}
226
227/// CountOperands The inputs to target nodes have any actual inputs first,
228/// followed by an optional chain operand, then flag operands. Compute the
229/// number of actual operands that will go into the machine instr.
Evan Cheng95f6ede2006-11-04 09:44:31 +0000230unsigned ScheduleDAG::CountOperands(SDNode *Node) {
Jim Laskeyb6d4c2c2005-09-30 19:15:27 +0000231 unsigned N = Node->getNumOperands();
232 while (N && Node->getOperand(N - 1).getValueType() == MVT::Flag)
Jim Laskeye6b90fb2005-09-26 21:57:04 +0000233 --N;
Jim Laskeyb6d4c2c2005-09-30 19:15:27 +0000234 if (N && Node->getOperand(N - 1).getValueType() == MVT::Other)
Jim Laskeye6b90fb2005-09-26 21:57:04 +0000235 --N; // Ignore chain if it exists.
236 return N;
237}
238
Jim Laskey60f09922006-07-21 20:57:35 +0000239static const TargetRegisterClass *getInstrOperandRegClass(
240 const MRegisterInfo *MRI,
241 const TargetInstrInfo *TII,
242 const TargetInstrDescriptor *II,
243 unsigned Op) {
244 if (Op >= II->numOperands) {
245 assert((II->Flags & M_VARIABLE_OPS)&& "Invalid operand # of instruction");
246 return NULL;
247 }
248 const TargetOperandInfo &toi = II->OpInfo[Op];
249 return (toi.Flags & M_LOOK_UP_PTR_REG_CLASS)
250 ? TII->getPointerRegClass() : MRI->getRegClass(toi.RegClass);
251}
252
253static unsigned CreateVirtualRegisters(const MRegisterInfo *MRI,
254 MachineInstr *MI,
Evan Cheng4ef10862006-01-23 07:01:07 +0000255 unsigned NumResults,
256 SSARegMap *RegMap,
Evan Cheng21d03f22006-05-18 20:42:07 +0000257 const TargetInstrInfo *TII,
Evan Cheng4ef10862006-01-23 07:01:07 +0000258 const TargetInstrDescriptor &II) {
Jim Laskeye6b90fb2005-09-26 21:57:04 +0000259 // Create the result registers for this node and add the result regs to
260 // the machine instruction.
Evan Cheng21d03f22006-05-18 20:42:07 +0000261 unsigned ResultReg =
Jim Laskey60f09922006-07-21 20:57:35 +0000262 RegMap->createVirtualRegister(getInstrOperandRegClass(MRI, TII, &II, 0));
Chris Lattner09e46062006-09-05 02:31:13 +0000263 MI->addRegOperand(ResultReg, true);
Jim Laskeye6b90fb2005-09-26 21:57:04 +0000264 for (unsigned i = 1; i != NumResults; ++i) {
Jim Laskey60f09922006-07-21 20:57:35 +0000265 const TargetRegisterClass *RC = getInstrOperandRegClass(MRI, TII, &II, i);
Evan Cheng21d03f22006-05-18 20:42:07 +0000266 assert(RC && "Isn't a register operand!");
Chris Lattner09e46062006-09-05 02:31:13 +0000267 MI->addRegOperand(RegMap->createVirtualRegister(RC), true);
Jim Laskeye6b90fb2005-09-26 21:57:04 +0000268 }
269 return ResultReg;
270}
271
Chris Lattnerdf375062006-03-10 07:25:12 +0000272/// getVR - Return the virtual register corresponding to the specified result
273/// of the specified node.
274static unsigned getVR(SDOperand Op, std::map<SDNode*, unsigned> &VRBaseMap) {
275 std::map<SDNode*, unsigned>::iterator I = VRBaseMap.find(Op.Val);
276 assert(I != VRBaseMap.end() && "Node emitted out of order - late");
277 return I->second + Op.ResNo;
278}
279
280
Chris Lattnered18b682006-02-24 18:54:03 +0000281/// AddOperand - Add the specified operand to the specified machine instr. II
282/// specifies the instruction information for the node, and IIOpNum is the
283/// operand number (in the II) that we are adding. IIOpNum and II are used for
284/// assertions only.
285void ScheduleDAG::AddOperand(MachineInstr *MI, SDOperand Op,
286 unsigned IIOpNum,
Chris Lattnerdf375062006-03-10 07:25:12 +0000287 const TargetInstrDescriptor *II,
288 std::map<SDNode*, unsigned> &VRBaseMap) {
Chris Lattnered18b682006-02-24 18:54:03 +0000289 if (Op.isTargetOpcode()) {
290 // Note that this case is redundant with the final else block, but we
291 // include it because it is the most common and it makes the logic
292 // simpler here.
293 assert(Op.getValueType() != MVT::Other &&
294 Op.getValueType() != MVT::Flag &&
295 "Chain and flag operands should occur at end of operand list!");
296
297 // Get/emit the operand.
Chris Lattnerdf375062006-03-10 07:25:12 +0000298 unsigned VReg = getVR(Op, VRBaseMap);
Chris Lattner09e46062006-09-05 02:31:13 +0000299 MI->addRegOperand(VReg, false);
Chris Lattnered18b682006-02-24 18:54:03 +0000300
301 // Verify that it is right.
302 assert(MRegisterInfo::isVirtualRegister(VReg) && "Not a vreg?");
303 if (II) {
Jim Laskey60f09922006-07-21 20:57:35 +0000304 const TargetRegisterClass *RC =
305 getInstrOperandRegClass(MRI, TII, II, IIOpNum);
Evan Cheng21d03f22006-05-18 20:42:07 +0000306 assert(RC && "Don't have operand info for this instruction!");
307 assert(RegMap->getRegClass(VReg) == RC &&
Chris Lattnered18b682006-02-24 18:54:03 +0000308 "Register class of operand and regclass of use don't agree!");
309 }
310 } else if (ConstantSDNode *C =
311 dyn_cast<ConstantSDNode>(Op)) {
Chris Lattner2d90ac72006-05-04 18:05:43 +0000312 MI->addImmOperand(C->getValue());
Chris Lattnered18b682006-02-24 18:54:03 +0000313 } else if (RegisterSDNode*R =
314 dyn_cast<RegisterSDNode>(Op)) {
Chris Lattner09e46062006-09-05 02:31:13 +0000315 MI->addRegOperand(R->getReg(), false);
Chris Lattnered18b682006-02-24 18:54:03 +0000316 } else if (GlobalAddressSDNode *TGA =
317 dyn_cast<GlobalAddressSDNode>(Op)) {
Chris Lattnerea50fab2006-05-04 01:15:02 +0000318 MI->addGlobalAddressOperand(TGA->getGlobal(), TGA->getOffset());
Chris Lattnered18b682006-02-24 18:54:03 +0000319 } else if (BasicBlockSDNode *BB =
320 dyn_cast<BasicBlockSDNode>(Op)) {
321 MI->addMachineBasicBlockOperand(BB->getBasicBlock());
322 } else if (FrameIndexSDNode *FI =
323 dyn_cast<FrameIndexSDNode>(Op)) {
324 MI->addFrameIndexOperand(FI->getIndex());
Nate Begeman37efe672006-04-22 18:53:45 +0000325 } else if (JumpTableSDNode *JT =
326 dyn_cast<JumpTableSDNode>(Op)) {
327 MI->addJumpTableIndexOperand(JT->getIndex());
Chris Lattnered18b682006-02-24 18:54:03 +0000328 } else if (ConstantPoolSDNode *CP =
329 dyn_cast<ConstantPoolSDNode>(Op)) {
Evan Cheng404cb4f2006-02-25 09:54:52 +0000330 int Offset = CP->getOffset();
Chris Lattnered18b682006-02-24 18:54:03 +0000331 unsigned Align = CP->getAlignment();
Evan Chengd6594ae2006-09-12 21:00:35 +0000332 const Type *Type = CP->getType();
Chris Lattnered18b682006-02-24 18:54:03 +0000333 // MachineConstantPool wants an explicit alignment.
334 if (Align == 0) {
Evan Chengd6594ae2006-09-12 21:00:35 +0000335 if (Type == Type::DoubleTy)
Chris Lattnered18b682006-02-24 18:54:03 +0000336 Align = 3; // always 8-byte align doubles.
Chris Lattner54a30b92006-03-20 01:51:46 +0000337 else {
Evan Chengd6594ae2006-09-12 21:00:35 +0000338 Align = TM.getTargetData()->getTypeAlignmentShift(Type);
Chris Lattner54a30b92006-03-20 01:51:46 +0000339 if (Align == 0) {
340 // Alignment of packed types. FIXME!
Evan Chengd6594ae2006-09-12 21:00:35 +0000341 Align = TM.getTargetData()->getTypeSize(Type);
Chris Lattner54a30b92006-03-20 01:51:46 +0000342 Align = Log2_64(Align);
343 }
344 }
Chris Lattnered18b682006-02-24 18:54:03 +0000345 }
346
Evan Chengd6594ae2006-09-12 21:00:35 +0000347 unsigned Idx;
348 if (CP->isMachineConstantPoolEntry())
349 Idx = ConstPool->getConstantPoolIndex(CP->getMachineCPVal(), Align);
350 else
351 Idx = ConstPool->getConstantPoolIndex(CP->getConstVal(), Align);
Evan Cheng404cb4f2006-02-25 09:54:52 +0000352 MI->addConstantPoolIndexOperand(Idx, Offset);
Chris Lattnered18b682006-02-24 18:54:03 +0000353 } else if (ExternalSymbolSDNode *ES =
354 dyn_cast<ExternalSymbolSDNode>(Op)) {
Chris Lattnerea50fab2006-05-04 01:15:02 +0000355 MI->addExternalSymbolOperand(ES->getSymbol());
Chris Lattnered18b682006-02-24 18:54:03 +0000356 } else {
357 assert(Op.getValueType() != MVT::Other &&
358 Op.getValueType() != MVT::Flag &&
359 "Chain and flag operands should occur at end of operand list!");
Chris Lattnerdf375062006-03-10 07:25:12 +0000360 unsigned VReg = getVR(Op, VRBaseMap);
Chris Lattner09e46062006-09-05 02:31:13 +0000361 MI->addRegOperand(VReg, false);
Chris Lattnered18b682006-02-24 18:54:03 +0000362
363 // Verify that it is right.
364 assert(MRegisterInfo::isVirtualRegister(VReg) && "Not a vreg?");
365 if (II) {
Jim Laskey60f09922006-07-21 20:57:35 +0000366 const TargetRegisterClass *RC =
367 getInstrOperandRegClass(MRI, TII, II, IIOpNum);
Evan Cheng21d03f22006-05-18 20:42:07 +0000368 assert(RC && "Don't have operand info for this instruction!");
369 assert(RegMap->getRegClass(VReg) == RC &&
Chris Lattnered18b682006-02-24 18:54:03 +0000370 "Register class of operand and regclass of use don't agree!");
371 }
372 }
373
374}
375
376
Jim Laskeyb6d4c2c2005-09-30 19:15:27 +0000377/// EmitNode - Generate machine code for an node and needed dependencies.
Jim Laskeye6b90fb2005-09-26 21:57:04 +0000378///
Chris Lattner8c7ef052006-03-10 07:28:36 +0000379void ScheduleDAG::EmitNode(SDNode *Node,
Chris Lattnerdf375062006-03-10 07:25:12 +0000380 std::map<SDNode*, unsigned> &VRBaseMap) {
Jim Laskeyb6d4c2c2005-09-30 19:15:27 +0000381 unsigned VRBase = 0; // First virtual register for node
Chris Lattner2d973e42005-08-18 20:07:59 +0000382
Jim Laskeyb6d4c2c2005-09-30 19:15:27 +0000383 // If machine instruction
384 if (Node->isTargetOpcode()) {
385 unsigned Opc = Node->getTargetOpcode();
Evan Chenga9c20912006-01-21 02:32:06 +0000386 const TargetInstrDescriptor &II = TII->get(Opc);
Chris Lattner2d973e42005-08-18 20:07:59 +0000387
Jim Laskeyb6d4c2c2005-09-30 19:15:27 +0000388 unsigned NumResults = CountResults(Node);
389 unsigned NodeOperands = CountOperands(Node);
Jim Laskeye6b90fb2005-09-26 21:57:04 +0000390 unsigned NumMIOperands = NodeOperands + NumResults;
Chris Lattnerda8abb02005-09-01 18:44:10 +0000391#ifndef NDEBUG
Evan Cheng8d3af5e2006-06-15 07:22:16 +0000392 assert((unsigned(II.numOperands) == NumMIOperands ||
393 (II.Flags & M_VARIABLE_OPS)) &&
Chris Lattner2d973e42005-08-18 20:07:59 +0000394 "#operands for dag node doesn't match .td file!");
Chris Lattnerca6aa2f2005-08-19 01:01:34 +0000395#endif
Chris Lattner2d973e42005-08-18 20:07:59 +0000396
397 // Create the new machine instruction.
Chris Lattner8b915b42006-05-04 18:16:01 +0000398 MachineInstr *MI = new MachineInstr(Opc, NumMIOperands);
Chris Lattner2d973e42005-08-18 20:07:59 +0000399
400 // Add result register values for things that are defined by this
401 // instruction.
Chris Lattnera4176522005-10-30 18:54:27 +0000402
403 // If the node is only used by a CopyToReg and the dest reg is a vreg, use
404 // the CopyToReg'd destination register instead of creating a new vreg.
405 if (NumResults == 1) {
406 for (SDNode::use_iterator UI = Node->use_begin(), E = Node->use_end();
407 UI != E; ++UI) {
408 SDNode *Use = *UI;
409 if (Use->getOpcode() == ISD::CopyToReg &&
410 Use->getOperand(2).Val == Node) {
411 unsigned Reg = cast<RegisterSDNode>(Use->getOperand(1))->getReg();
412 if (MRegisterInfo::isVirtualRegister(Reg)) {
413 VRBase = Reg;
Chris Lattner09e46062006-09-05 02:31:13 +0000414 MI->addRegOperand(Reg, true);
Chris Lattnera4176522005-10-30 18:54:27 +0000415 break;
416 }
417 }
418 }
419 }
420
421 // Otherwise, create new virtual registers.
422 if (NumResults && VRBase == 0)
Jim Laskey60f09922006-07-21 20:57:35 +0000423 VRBase = CreateVirtualRegisters(MRI, MI, NumResults, RegMap, TII, II);
Jim Laskeyb6d4c2c2005-09-30 19:15:27 +0000424
425 // Emit all of the actual operands of this instruction, adding them to the
426 // instruction as appropriate.
Chris Lattnered18b682006-02-24 18:54:03 +0000427 for (unsigned i = 0; i != NodeOperands; ++i)
Chris Lattnerdf375062006-03-10 07:25:12 +0000428 AddOperand(MI, Node->getOperand(i), i+NumResults, &II, VRBaseMap);
Evan Cheng13d41b92006-05-12 01:58:24 +0000429
430 // Commute node if it has been determined to be profitable.
431 if (CommuteSet.count(Node)) {
432 MachineInstr *NewMI = TII->commuteInstruction(MI);
433 if (NewMI == 0)
434 DEBUG(std::cerr << "Sched: COMMUTING FAILED!\n");
435 else {
436 DEBUG(std::cerr << "Sched: COMMUTED TO: " << *NewMI);
Evan Cheng4c6f2f92006-05-31 18:03:39 +0000437 if (MI != NewMI) {
438 delete MI;
439 MI = NewMI;
440 }
Evan Cheng13d41b92006-05-12 01:58:24 +0000441 }
442 }
443
Jim Laskeyb6d4c2c2005-09-30 19:15:27 +0000444 // Now that we have emitted all operands, emit this instruction itself.
445 if ((II.Flags & M_USES_CUSTOM_DAG_SCHED_INSERTION) == 0) {
446 BB->insert(BB->end(), MI);
447 } else {
448 // Insert this instruction into the end of the basic block, potentially
449 // taking some custom action.
450 BB = DAG.getTargetLoweringInfo().InsertAtEndOfBasicBlock(MI, BB);
451 }
452 } else {
453 switch (Node->getOpcode()) {
454 default:
Jim Laskey16d42c62006-07-11 18:25:13 +0000455#ifndef NDEBUG
456 Node->dump();
457#endif
Jim Laskeyb6d4c2c2005-09-30 19:15:27 +0000458 assert(0 && "This target-independent node should have been selected!");
459 case ISD::EntryToken: // fall thru
460 case ISD::TokenFactor:
461 break;
462 case ISD::CopyToReg: {
Chris Lattnerdf375062006-03-10 07:25:12 +0000463 unsigned InReg = getVR(Node->getOperand(2), VRBaseMap);
Chris Lattnera4176522005-10-30 18:54:27 +0000464 unsigned DestReg = cast<RegisterSDNode>(Node->getOperand(1))->getReg();
Chris Lattner45053fc2006-03-24 07:15:07 +0000465 if (InReg != DestReg) // Coalesced away the copy?
Evan Chenga9c20912006-01-21 02:32:06 +0000466 MRI->copyRegToReg(*BB, BB->end(), DestReg, InReg,
467 RegMap->getRegClass(InReg));
Jim Laskeyb6d4c2c2005-09-30 19:15:27 +0000468 break;
469 }
470 case ISD::CopyFromReg: {
471 unsigned SrcReg = cast<RegisterSDNode>(Node->getOperand(1))->getReg();
Chris Lattner089c25c2005-10-09 05:58:56 +0000472 if (MRegisterInfo::isVirtualRegister(SrcReg)) {
473 VRBase = SrcReg; // Just use the input register directly!
474 break;
475 }
476
Chris Lattnera4176522005-10-30 18:54:27 +0000477 // If the node is only used by a CopyToReg and the dest reg is a vreg, use
478 // the CopyToReg'd destination register instead of creating a new vreg.
479 for (SDNode::use_iterator UI = Node->use_begin(), E = Node->use_end();
480 UI != E; ++UI) {
481 SDNode *Use = *UI;
482 if (Use->getOpcode() == ISD::CopyToReg &&
483 Use->getOperand(2).Val == Node) {
484 unsigned DestReg = cast<RegisterSDNode>(Use->getOperand(1))->getReg();
485 if (MRegisterInfo::isVirtualRegister(DestReg)) {
486 VRBase = DestReg;
487 break;
488 }
489 }
490 }
491
Jim Laskeyb6d4c2c2005-09-30 19:15:27 +0000492 // Figure out the register class to create for the destreg.
493 const TargetRegisterClass *TRC = 0;
Chris Lattnera4176522005-10-30 18:54:27 +0000494 if (VRBase) {
495 TRC = RegMap->getRegClass(VRBase);
496 } else {
Chris Lattner089c25c2005-10-09 05:58:56 +0000497
Chris Lattnera4176522005-10-30 18:54:27 +0000498 // Pick the register class of the right type that contains this physreg.
Evan Chenga9c20912006-01-21 02:32:06 +0000499 for (MRegisterInfo::regclass_iterator I = MRI->regclass_begin(),
500 E = MRI->regclass_end(); I != E; ++I)
Nate Begeman6510b222005-12-01 04:51:06 +0000501 if ((*I)->hasType(Node->getValueType(0)) &&
Chris Lattnera4176522005-10-30 18:54:27 +0000502 (*I)->contains(SrcReg)) {
503 TRC = *I;
504 break;
505 }
506 assert(TRC && "Couldn't find register class for reg copy!");
Jim Laskeyb6d4c2c2005-09-30 19:15:27 +0000507
Chris Lattnera4176522005-10-30 18:54:27 +0000508 // Create the reg, emit the copy.
509 VRBase = RegMap->createVirtualRegister(TRC);
510 }
Evan Chenga9c20912006-01-21 02:32:06 +0000511 MRI->copyRegToReg(*BB, BB->end(), VRBase, SrcReg, TRC);
Jim Laskeyb6d4c2c2005-09-30 19:15:27 +0000512 break;
513 }
Chris Lattneracc43bf2006-01-26 23:28:04 +0000514 case ISD::INLINEASM: {
515 unsigned NumOps = Node->getNumOperands();
516 if (Node->getOperand(NumOps-1).getValueType() == MVT::Flag)
517 --NumOps; // Ignore the flag operand.
518
519 // Create the inline asm machine instruction.
520 MachineInstr *MI =
521 new MachineInstr(BB, TargetInstrInfo::INLINEASM, (NumOps-2)/2+1);
522
523 // Add the asm string as an external symbol operand.
524 const char *AsmStr =
525 cast<ExternalSymbolSDNode>(Node->getOperand(1))->getSymbol();
Chris Lattnerea50fab2006-05-04 01:15:02 +0000526 MI->addExternalSymbolOperand(AsmStr);
Chris Lattneracc43bf2006-01-26 23:28:04 +0000527
528 // Add all of the operand registers to the instruction.
Chris Lattnerc3a9f8d2006-02-23 19:21:04 +0000529 for (unsigned i = 2; i != NumOps;) {
530 unsigned Flags = cast<ConstantSDNode>(Node->getOperand(i))->getValue();
Chris Lattnerfd6d2822006-02-24 19:18:20 +0000531 unsigned NumVals = Flags >> 3;
Chris Lattnerc3a9f8d2006-02-23 19:21:04 +0000532
Chris Lattner2d90ac72006-05-04 18:05:43 +0000533 MI->addImmOperand(Flags);
Chris Lattnerc3a9f8d2006-02-23 19:21:04 +0000534 ++i; // Skip the ID value.
535
536 switch (Flags & 7) {
Chris Lattneracc43bf2006-01-26 23:28:04 +0000537 default: assert(0 && "Bad flags!");
Chris Lattnerc3a9f8d2006-02-23 19:21:04 +0000538 case 1: // Use of register.
Chris Lattnerfd6d2822006-02-24 19:18:20 +0000539 for (; NumVals; --NumVals, ++i) {
Chris Lattnerc3a9f8d2006-02-23 19:21:04 +0000540 unsigned Reg = cast<RegisterSDNode>(Node->getOperand(i))->getReg();
Chris Lattner09e46062006-09-05 02:31:13 +0000541 MI->addRegOperand(Reg, false);
Chris Lattnerc3a9f8d2006-02-23 19:21:04 +0000542 }
Chris Lattnerdc19b702006-02-04 02:26:14 +0000543 break;
Chris Lattnerc3a9f8d2006-02-23 19:21:04 +0000544 case 2: // Def of register.
Chris Lattnerfd6d2822006-02-24 19:18:20 +0000545 for (; NumVals; --NumVals, ++i) {
Chris Lattnerc3a9f8d2006-02-23 19:21:04 +0000546 unsigned Reg = cast<RegisterSDNode>(Node->getOperand(i))->getReg();
Chris Lattner09e46062006-09-05 02:31:13 +0000547 MI->addRegOperand(Reg, true);
Chris Lattnerc3a9f8d2006-02-23 19:21:04 +0000548 }
Chris Lattnerdc19b702006-02-04 02:26:14 +0000549 break;
Chris Lattnerdc19b702006-02-04 02:26:14 +0000550 case 3: { // Immediate.
Chris Lattnerfd6d2822006-02-24 19:18:20 +0000551 assert(NumVals == 1 && "Unknown immediate value!");
Chris Lattnerefa46ce2006-10-31 20:01:56 +0000552 if (ConstantSDNode *CS=dyn_cast<ConstantSDNode>(Node->getOperand(i))){
553 MI->addImmOperand(CS->getValue());
554 } else {
555 GlobalAddressSDNode *GA =
556 cast<GlobalAddressSDNode>(Node->getOperand(i));
557 MI->addGlobalAddressOperand(GA->getGlobal(), GA->getOffset());
558 }
Chris Lattnerc3a9f8d2006-02-23 19:21:04 +0000559 ++i;
Chris Lattnerdc19b702006-02-04 02:26:14 +0000560 break;
561 }
Chris Lattnerfd6d2822006-02-24 19:18:20 +0000562 case 4: // Addressing mode.
563 // The addressing mode has been selected, just add all of the
564 // operands to the machine instruction.
565 for (; NumVals; --NumVals, ++i)
Chris Lattnerdf375062006-03-10 07:25:12 +0000566 AddOperand(MI, Node->getOperand(i), 0, 0, VRBaseMap);
Chris Lattnerfd6d2822006-02-24 19:18:20 +0000567 break;
Chris Lattnerdc19b702006-02-04 02:26:14 +0000568 }
Chris Lattneracc43bf2006-01-26 23:28:04 +0000569 }
570 break;
571 }
Jim Laskeyb6d4c2c2005-09-30 19:15:27 +0000572 }
573 }
574
Chris Lattnerdf375062006-03-10 07:25:12 +0000575 assert(!VRBaseMap.count(Node) && "Node emitted out of order - early");
576 VRBaseMap[Node] = VRBase;
Jim Laskeyb6d4c2c2005-09-30 19:15:27 +0000577}
578
Chris Lattnera93dfcd2006-03-05 23:51:47 +0000579void ScheduleDAG::EmitNoop() {
580 TII->insertNoop(*BB, BB->end());
581}
582
Evan Chenge165a782006-05-11 23:55:42 +0000583/// EmitSchedule - Emit the machine code in scheduled order.
584void ScheduleDAG::EmitSchedule() {
Chris Lattner96645412006-05-16 06:10:58 +0000585 // If this is the first basic block in the function, and if it has live ins
586 // that need to be copied into vregs, emit the copies into the top of the
587 // block before emitting the code for the block.
588 MachineFunction &MF = DAG.getMachineFunction();
589 if (&MF.front() == BB && MF.livein_begin() != MF.livein_end()) {
590 for (MachineFunction::livein_iterator LI = MF.livein_begin(),
591 E = MF.livein_end(); LI != E; ++LI)
592 if (LI->second)
593 MRI->copyRegToReg(*MF.begin(), MF.begin()->end(), LI->second,
594 LI->first, RegMap->getRegClass(LI->second));
595 }
596
597
598 // Finally, emit the code for all of the scheduled instructions.
Evan Chenge165a782006-05-11 23:55:42 +0000599 std::map<SDNode*, unsigned> VRBaseMap;
600 for (unsigned i = 0, e = Sequence.size(); i != e; i++) {
601 if (SUnit *SU = Sequence[i]) {
602 for (unsigned j = 0, ee = SU->FlaggedNodes.size(); j != ee; j++)
603 EmitNode(SU->FlaggedNodes[j], VRBaseMap);
604 EmitNode(SU->Node, VRBaseMap);
605 } else {
606 // Null SUnit* is a noop.
607 EmitNoop();
608 }
609 }
610}
611
612/// dump - dump the schedule.
613void ScheduleDAG::dumpSchedule() const {
614 for (unsigned i = 0, e = Sequence.size(); i != e; i++) {
615 if (SUnit *SU = Sequence[i])
616 SU->dump(&DAG);
617 else
618 std::cerr << "**** NOOP ****\n";
619 }
620}
621
622
Evan Chenga9c20912006-01-21 02:32:06 +0000623/// Run - perform scheduling.
624///
625MachineBasicBlock *ScheduleDAG::Run() {
626 TII = TM.getInstrInfo();
627 MRI = TM.getRegisterInfo();
628 RegMap = BB->getParent()->getSSARegMap();
629 ConstPool = BB->getParent()->getConstantPool();
Evan Cheng4ef10862006-01-23 07:01:07 +0000630
Evan Chenga9c20912006-01-21 02:32:06 +0000631 Schedule();
632 return BB;
Chris Lattnerd32b2362005-08-18 18:45:24 +0000633}
Evan Cheng4ef10862006-01-23 07:01:07 +0000634
Evan Chenge165a782006-05-11 23:55:42 +0000635/// SUnit - Scheduling unit. It's an wrapper around either a single SDNode or
636/// a group of nodes flagged together.
637void SUnit::dump(const SelectionDAG *G) const {
638 std::cerr << "SU(" << NodeNum << "): ";
639 Node->dump(G);
640 std::cerr << "\n";
641 if (FlaggedNodes.size() != 0) {
642 for (unsigned i = 0, e = FlaggedNodes.size(); i != e; i++) {
643 std::cerr << " ";
644 FlaggedNodes[i]->dump(G);
645 std::cerr << "\n";
646 }
647 }
648}
Evan Cheng4ef10862006-01-23 07:01:07 +0000649
Evan Chenge165a782006-05-11 23:55:42 +0000650void SUnit::dumpAll(const SelectionDAG *G) const {
651 dump(G);
652
653 std::cerr << " # preds left : " << NumPredsLeft << "\n";
654 std::cerr << " # succs left : " << NumSuccsLeft << "\n";
655 std::cerr << " # chain preds left : " << NumChainPredsLeft << "\n";
656 std::cerr << " # chain succs left : " << NumChainSuccsLeft << "\n";
657 std::cerr << " Latency : " << Latency << "\n";
658 std::cerr << " Depth : " << Depth << "\n";
659 std::cerr << " Height : " << Height << "\n";
660
661 if (Preds.size() != 0) {
662 std::cerr << " Predecessors:\n";
Chris Lattner228a18e2006-08-17 00:09:56 +0000663 for (SUnit::const_succ_iterator I = Preds.begin(), E = Preds.end();
664 I != E; ++I) {
Evan Chenge165a782006-05-11 23:55:42 +0000665 if (I->second)
Chris Lattner228a18e2006-08-17 00:09:56 +0000666 std::cerr << " ch #";
Evan Chenge165a782006-05-11 23:55:42 +0000667 else
Chris Lattner228a18e2006-08-17 00:09:56 +0000668 std::cerr << " val #";
Evan Chengd42a5232006-10-14 08:34:06 +0000669 std::cerr << I->first << " - SU(" << I->first->NodeNum << ")\n";
Evan Chenge165a782006-05-11 23:55:42 +0000670 }
671 }
672 if (Succs.size() != 0) {
673 std::cerr << " Successors:\n";
Chris Lattner228a18e2006-08-17 00:09:56 +0000674 for (SUnit::const_succ_iterator I = Succs.begin(), E = Succs.end();
675 I != E; ++I) {
Evan Chenge165a782006-05-11 23:55:42 +0000676 if (I->second)
Chris Lattner228a18e2006-08-17 00:09:56 +0000677 std::cerr << " ch #";
Evan Chenge165a782006-05-11 23:55:42 +0000678 else
Chris Lattner228a18e2006-08-17 00:09:56 +0000679 std::cerr << " val #";
Evan Chengd42a5232006-10-14 08:34:06 +0000680 std::cerr << I->first << " - SU(" << I->first->NodeNum << ")\n";
Evan Chenge165a782006-05-11 23:55:42 +0000681 }
682 }
683 std::cerr << "\n";
684}