blob: bf95a92555afc73b02a4fdd7337a77f3da2abf6b [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
Chris Lattnerb0d21ef2006-03-08 04:25:59 +000016#include "llvm/CodeGen/ScheduleDAG.h"
Chris Lattner5839bf22005-08-26 17:15:30 +000017#include "llvm/CodeGen/MachineConstantPool.h"
Chris Lattner4ccd4062005-08-19 20:45:43 +000018#include "llvm/CodeGen/MachineFunction.h"
Chris Lattner4ccd4062005-08-19 20:45:43 +000019#include "llvm/CodeGen/SSARegMap.h"
Chris Lattner2d973e42005-08-18 20:07:59 +000020#include "llvm/Target/TargetMachine.h"
21#include "llvm/Target/TargetInstrInfo.h"
Chris Lattner025c39b2005-08-26 20:54:47 +000022#include "llvm/Target/TargetLowering.h"
Chris Lattner54a30b92006-03-20 01:51:46 +000023#include "llvm/Support/MathExtras.h"
Chris Lattnerd32b2362005-08-18 18:45:24 +000024using namespace llvm;
25
Jim Laskeye6b90fb2005-09-26 21:57:04 +000026
27/// CountResults - The results of target nodes have register or immediate
28/// operands first, then an optional chain, and optional flag operands (which do
29/// not go into the machine instrs.)
Evan Chenga9c20912006-01-21 02:32:06 +000030static unsigned CountResults(SDNode *Node) {
Jim Laskeyb6d4c2c2005-09-30 19:15:27 +000031 unsigned N = Node->getNumValues();
32 while (N && Node->getValueType(N - 1) == MVT::Flag)
Jim Laskeye6b90fb2005-09-26 21:57:04 +000033 --N;
Jim Laskeyb6d4c2c2005-09-30 19:15:27 +000034 if (N && Node->getValueType(N - 1) == MVT::Other)
Jim Laskeye6b90fb2005-09-26 21:57:04 +000035 --N; // Skip over chain result.
36 return N;
37}
38
39/// CountOperands The inputs to target nodes have any actual inputs first,
40/// followed by an optional chain operand, then flag operands. Compute the
41/// number of actual operands that will go into the machine instr.
Evan Chenga9c20912006-01-21 02:32:06 +000042static unsigned CountOperands(SDNode *Node) {
Jim Laskeyb6d4c2c2005-09-30 19:15:27 +000043 unsigned N = Node->getNumOperands();
44 while (N && Node->getOperand(N - 1).getValueType() == MVT::Flag)
Jim Laskeye6b90fb2005-09-26 21:57:04 +000045 --N;
Jim Laskeyb6d4c2c2005-09-30 19:15:27 +000046 if (N && Node->getOperand(N - 1).getValueType() == MVT::Other)
Jim Laskeye6b90fb2005-09-26 21:57:04 +000047 --N; // Ignore chain if it exists.
48 return N;
49}
50
Evan Cheng4ef10862006-01-23 07:01:07 +000051static unsigned CreateVirtualRegisters(MachineInstr *MI,
52 unsigned NumResults,
53 SSARegMap *RegMap,
54 const TargetInstrDescriptor &II) {
Jim Laskeye6b90fb2005-09-26 21:57:04 +000055 // Create the result registers for this node and add the result regs to
56 // the machine instruction.
57 const TargetOperandInfo *OpInfo = II.OpInfo;
58 unsigned ResultReg = RegMap->createVirtualRegister(OpInfo[0].RegClass);
59 MI->addRegOperand(ResultReg, MachineOperand::Def);
60 for (unsigned i = 1; i != NumResults; ++i) {
61 assert(OpInfo[i].RegClass && "Isn't a register operand!");
Chris Lattner505277a2005-10-01 07:45:09 +000062 MI->addRegOperand(RegMap->createVirtualRegister(OpInfo[i].RegClass),
Jim Laskeye6b90fb2005-09-26 21:57:04 +000063 MachineOperand::Def);
64 }
65 return ResultReg;
66}
67
Chris Lattnerdf375062006-03-10 07:25:12 +000068/// getVR - Return the virtual register corresponding to the specified result
69/// of the specified node.
70static unsigned getVR(SDOperand Op, std::map<SDNode*, unsigned> &VRBaseMap) {
71 std::map<SDNode*, unsigned>::iterator I = VRBaseMap.find(Op.Val);
72 assert(I != VRBaseMap.end() && "Node emitted out of order - late");
73 return I->second + Op.ResNo;
74}
75
76
Chris Lattnered18b682006-02-24 18:54:03 +000077/// AddOperand - Add the specified operand to the specified machine instr. II
78/// specifies the instruction information for the node, and IIOpNum is the
79/// operand number (in the II) that we are adding. IIOpNum and II are used for
80/// assertions only.
81void ScheduleDAG::AddOperand(MachineInstr *MI, SDOperand Op,
82 unsigned IIOpNum,
Chris Lattnerdf375062006-03-10 07:25:12 +000083 const TargetInstrDescriptor *II,
84 std::map<SDNode*, unsigned> &VRBaseMap) {
Chris Lattnered18b682006-02-24 18:54:03 +000085 if (Op.isTargetOpcode()) {
86 // Note that this case is redundant with the final else block, but we
87 // include it because it is the most common and it makes the logic
88 // simpler here.
89 assert(Op.getValueType() != MVT::Other &&
90 Op.getValueType() != MVT::Flag &&
91 "Chain and flag operands should occur at end of operand list!");
92
93 // Get/emit the operand.
Chris Lattnerdf375062006-03-10 07:25:12 +000094 unsigned VReg = getVR(Op, VRBaseMap);
Chris Lattnered18b682006-02-24 18:54:03 +000095 MI->addRegOperand(VReg, MachineOperand::Use);
96
97 // Verify that it is right.
98 assert(MRegisterInfo::isVirtualRegister(VReg) && "Not a vreg?");
99 if (II) {
100 assert(II->OpInfo[IIOpNum].RegClass &&
101 "Don't have operand info for this instruction!");
102 assert(RegMap->getRegClass(VReg) == II->OpInfo[IIOpNum].RegClass &&
103 "Register class of operand and regclass of use don't agree!");
104 }
105 } else if (ConstantSDNode *C =
106 dyn_cast<ConstantSDNode>(Op)) {
107 MI->addZeroExtImm64Operand(C->getValue());
108 } else if (RegisterSDNode*R =
109 dyn_cast<RegisterSDNode>(Op)) {
110 MI->addRegOperand(R->getReg(), MachineOperand::Use);
111 } else if (GlobalAddressSDNode *TGA =
112 dyn_cast<GlobalAddressSDNode>(Op)) {
113 MI->addGlobalAddressOperand(TGA->getGlobal(), false, TGA->getOffset());
114 } else if (BasicBlockSDNode *BB =
115 dyn_cast<BasicBlockSDNode>(Op)) {
116 MI->addMachineBasicBlockOperand(BB->getBasicBlock());
117 } else if (FrameIndexSDNode *FI =
118 dyn_cast<FrameIndexSDNode>(Op)) {
119 MI->addFrameIndexOperand(FI->getIndex());
Nate Begeman37efe672006-04-22 18:53:45 +0000120 } else if (JumpTableSDNode *JT =
121 dyn_cast<JumpTableSDNode>(Op)) {
122 MI->addJumpTableIndexOperand(JT->getIndex());
Chris Lattnered18b682006-02-24 18:54:03 +0000123 } else if (ConstantPoolSDNode *CP =
124 dyn_cast<ConstantPoolSDNode>(Op)) {
Evan Cheng404cb4f2006-02-25 09:54:52 +0000125 int Offset = CP->getOffset();
Chris Lattnered18b682006-02-24 18:54:03 +0000126 unsigned Align = CP->getAlignment();
127 // MachineConstantPool wants an explicit alignment.
128 if (Align == 0) {
129 if (CP->get()->getType() == Type::DoubleTy)
130 Align = 3; // always 8-byte align doubles.
Chris Lattner54a30b92006-03-20 01:51:46 +0000131 else {
Chris Lattnered18b682006-02-24 18:54:03 +0000132 Align = TM.getTargetData()
133 .getTypeAlignmentShift(CP->get()->getType());
Chris Lattner54a30b92006-03-20 01:51:46 +0000134 if (Align == 0) {
135 // Alignment of packed types. FIXME!
136 Align = TM.getTargetData().getTypeSize(CP->get()->getType());
137 Align = Log2_64(Align);
138 }
139 }
Chris Lattnered18b682006-02-24 18:54:03 +0000140 }
141
142 unsigned Idx = ConstPool->getConstantPoolIndex(CP->get(), Align);
Evan Cheng404cb4f2006-02-25 09:54:52 +0000143 MI->addConstantPoolIndexOperand(Idx, Offset);
Chris Lattnered18b682006-02-24 18:54:03 +0000144 } else if (ExternalSymbolSDNode *ES =
145 dyn_cast<ExternalSymbolSDNode>(Op)) {
146 MI->addExternalSymbolOperand(ES->getSymbol(), false);
147 } else {
148 assert(Op.getValueType() != MVT::Other &&
149 Op.getValueType() != MVT::Flag &&
150 "Chain and flag operands should occur at end of operand list!");
Chris Lattnerdf375062006-03-10 07:25:12 +0000151 unsigned VReg = getVR(Op, VRBaseMap);
Chris Lattnered18b682006-02-24 18:54:03 +0000152 MI->addRegOperand(VReg, MachineOperand::Use);
153
154 // Verify that it is right.
155 assert(MRegisterInfo::isVirtualRegister(VReg) && "Not a vreg?");
156 if (II) {
157 assert(II->OpInfo[IIOpNum].RegClass &&
158 "Don't have operand info for this instruction!");
159 assert(RegMap->getRegClass(VReg) == II->OpInfo[IIOpNum].RegClass &&
160 "Register class of operand and regclass of use don't agree!");
161 }
162 }
163
164}
165
166
Jim Laskeyb6d4c2c2005-09-30 19:15:27 +0000167/// EmitNode - Generate machine code for an node and needed dependencies.
Jim Laskeye6b90fb2005-09-26 21:57:04 +0000168///
Chris Lattner8c7ef052006-03-10 07:28:36 +0000169void ScheduleDAG::EmitNode(SDNode *Node,
Chris Lattnerdf375062006-03-10 07:25:12 +0000170 std::map<SDNode*, unsigned> &VRBaseMap) {
Jim Laskeyb6d4c2c2005-09-30 19:15:27 +0000171 unsigned VRBase = 0; // First virtual register for node
Chris Lattner2d973e42005-08-18 20:07:59 +0000172
Jim Laskeyb6d4c2c2005-09-30 19:15:27 +0000173 // If machine instruction
174 if (Node->isTargetOpcode()) {
175 unsigned Opc = Node->getTargetOpcode();
Evan Chenga9c20912006-01-21 02:32:06 +0000176 const TargetInstrDescriptor &II = TII->get(Opc);
Chris Lattner2d973e42005-08-18 20:07:59 +0000177
Jim Laskeyb6d4c2c2005-09-30 19:15:27 +0000178 unsigned NumResults = CountResults(Node);
179 unsigned NodeOperands = CountOperands(Node);
Jim Laskeye6b90fb2005-09-26 21:57:04 +0000180 unsigned NumMIOperands = NodeOperands + NumResults;
Chris Lattnerda8abb02005-09-01 18:44:10 +0000181#ifndef NDEBUG
Chris Lattner14b392a2005-08-24 22:02:41 +0000182 assert((unsigned(II.numOperands) == NumMIOperands || II.numOperands == -1)&&
Chris Lattner2d973e42005-08-18 20:07:59 +0000183 "#operands for dag node doesn't match .td file!");
Chris Lattnerca6aa2f2005-08-19 01:01:34 +0000184#endif
Chris Lattner2d973e42005-08-18 20:07:59 +0000185
186 // Create the new machine instruction.
Chris Lattner14b392a2005-08-24 22:02:41 +0000187 MachineInstr *MI = new MachineInstr(Opc, NumMIOperands, true, true);
Chris Lattner2d973e42005-08-18 20:07:59 +0000188
189 // Add result register values for things that are defined by this
190 // instruction.
Chris Lattnera4176522005-10-30 18:54:27 +0000191
192 // If the node is only used by a CopyToReg and the dest reg is a vreg, use
193 // the CopyToReg'd destination register instead of creating a new vreg.
194 if (NumResults == 1) {
195 for (SDNode::use_iterator UI = Node->use_begin(), E = Node->use_end();
196 UI != E; ++UI) {
197 SDNode *Use = *UI;
198 if (Use->getOpcode() == ISD::CopyToReg &&
199 Use->getOperand(2).Val == Node) {
200 unsigned Reg = cast<RegisterSDNode>(Use->getOperand(1))->getReg();
201 if (MRegisterInfo::isVirtualRegister(Reg)) {
202 VRBase = Reg;
203 MI->addRegOperand(Reg, MachineOperand::Def);
204 break;
205 }
206 }
207 }
208 }
209
210 // Otherwise, create new virtual registers.
211 if (NumResults && VRBase == 0)
Evan Cheng4ef10862006-01-23 07:01:07 +0000212 VRBase = CreateVirtualRegisters(MI, NumResults, RegMap, II);
Jim Laskeyb6d4c2c2005-09-30 19:15:27 +0000213
214 // Emit all of the actual operands of this instruction, adding them to the
215 // instruction as appropriate.
Chris Lattnered18b682006-02-24 18:54:03 +0000216 for (unsigned i = 0; i != NodeOperands; ++i)
Chris Lattnerdf375062006-03-10 07:25:12 +0000217 AddOperand(MI, Node->getOperand(i), i+NumResults, &II, VRBaseMap);
Jim Laskeyb6d4c2c2005-09-30 19:15:27 +0000218
219 // Now that we have emitted all operands, emit this instruction itself.
220 if ((II.Flags & M_USES_CUSTOM_DAG_SCHED_INSERTION) == 0) {
221 BB->insert(BB->end(), MI);
222 } else {
223 // Insert this instruction into the end of the basic block, potentially
224 // taking some custom action.
225 BB = DAG.getTargetLoweringInfo().InsertAtEndOfBasicBlock(MI, BB);
226 }
227 } else {
228 switch (Node->getOpcode()) {
229 default:
230 Node->dump();
231 assert(0 && "This target-independent node should have been selected!");
232 case ISD::EntryToken: // fall thru
233 case ISD::TokenFactor:
234 break;
235 case ISD::CopyToReg: {
Chris Lattnerdf375062006-03-10 07:25:12 +0000236 unsigned InReg = getVR(Node->getOperand(2), VRBaseMap);
Chris Lattnera4176522005-10-30 18:54:27 +0000237 unsigned DestReg = cast<RegisterSDNode>(Node->getOperand(1))->getReg();
Chris Lattner45053fc2006-03-24 07:15:07 +0000238 if (InReg != DestReg) // Coalesced away the copy?
Evan Chenga9c20912006-01-21 02:32:06 +0000239 MRI->copyRegToReg(*BB, BB->end(), DestReg, InReg,
240 RegMap->getRegClass(InReg));
Jim Laskeyb6d4c2c2005-09-30 19:15:27 +0000241 break;
242 }
243 case ISD::CopyFromReg: {
244 unsigned SrcReg = cast<RegisterSDNode>(Node->getOperand(1))->getReg();
Chris Lattner089c25c2005-10-09 05:58:56 +0000245 if (MRegisterInfo::isVirtualRegister(SrcReg)) {
246 VRBase = SrcReg; // Just use the input register directly!
247 break;
248 }
249
Chris Lattnera4176522005-10-30 18:54:27 +0000250 // If the node is only used by a CopyToReg and the dest reg is a vreg, use
251 // the CopyToReg'd destination register instead of creating a new vreg.
252 for (SDNode::use_iterator UI = Node->use_begin(), E = Node->use_end();
253 UI != E; ++UI) {
254 SDNode *Use = *UI;
255 if (Use->getOpcode() == ISD::CopyToReg &&
256 Use->getOperand(2).Val == Node) {
257 unsigned DestReg = cast<RegisterSDNode>(Use->getOperand(1))->getReg();
258 if (MRegisterInfo::isVirtualRegister(DestReg)) {
259 VRBase = DestReg;
260 break;
261 }
262 }
263 }
264
Jim Laskeyb6d4c2c2005-09-30 19:15:27 +0000265 // Figure out the register class to create for the destreg.
266 const TargetRegisterClass *TRC = 0;
Chris Lattnera4176522005-10-30 18:54:27 +0000267 if (VRBase) {
268 TRC = RegMap->getRegClass(VRBase);
269 } else {
Chris Lattner089c25c2005-10-09 05:58:56 +0000270
Chris Lattnera4176522005-10-30 18:54:27 +0000271 // Pick the register class of the right type that contains this physreg.
Evan Chenga9c20912006-01-21 02:32:06 +0000272 for (MRegisterInfo::regclass_iterator I = MRI->regclass_begin(),
273 E = MRI->regclass_end(); I != E; ++I)
Nate Begeman6510b222005-12-01 04:51:06 +0000274 if ((*I)->hasType(Node->getValueType(0)) &&
Chris Lattnera4176522005-10-30 18:54:27 +0000275 (*I)->contains(SrcReg)) {
276 TRC = *I;
277 break;
278 }
279 assert(TRC && "Couldn't find register class for reg copy!");
Jim Laskeyb6d4c2c2005-09-30 19:15:27 +0000280
Chris Lattnera4176522005-10-30 18:54:27 +0000281 // Create the reg, emit the copy.
282 VRBase = RegMap->createVirtualRegister(TRC);
283 }
Evan Chenga9c20912006-01-21 02:32:06 +0000284 MRI->copyRegToReg(*BB, BB->end(), VRBase, SrcReg, TRC);
Jim Laskeyb6d4c2c2005-09-30 19:15:27 +0000285 break;
286 }
Chris Lattneracc43bf2006-01-26 23:28:04 +0000287 case ISD::INLINEASM: {
288 unsigned NumOps = Node->getNumOperands();
289 if (Node->getOperand(NumOps-1).getValueType() == MVT::Flag)
290 --NumOps; // Ignore the flag operand.
291
292 // Create the inline asm machine instruction.
293 MachineInstr *MI =
294 new MachineInstr(BB, TargetInstrInfo::INLINEASM, (NumOps-2)/2+1);
295
296 // Add the asm string as an external symbol operand.
297 const char *AsmStr =
298 cast<ExternalSymbolSDNode>(Node->getOperand(1))->getSymbol();
299 MI->addExternalSymbolOperand(AsmStr, false);
300
301 // Add all of the operand registers to the instruction.
Chris Lattnerc3a9f8d2006-02-23 19:21:04 +0000302 for (unsigned i = 2; i != NumOps;) {
303 unsigned Flags = cast<ConstantSDNode>(Node->getOperand(i))->getValue();
Chris Lattnerfd6d2822006-02-24 19:18:20 +0000304 unsigned NumVals = Flags >> 3;
Chris Lattnerc3a9f8d2006-02-23 19:21:04 +0000305
Chris Lattnerdaf6bc62006-02-24 19:50:58 +0000306 MI->addZeroExtImm64Operand(Flags);
Chris Lattnerc3a9f8d2006-02-23 19:21:04 +0000307 ++i; // Skip the ID value.
308
309 switch (Flags & 7) {
Chris Lattneracc43bf2006-01-26 23:28:04 +0000310 default: assert(0 && "Bad flags!");
Chris Lattnerc3a9f8d2006-02-23 19:21:04 +0000311 case 1: // Use of register.
Chris Lattnerfd6d2822006-02-24 19:18:20 +0000312 for (; NumVals; --NumVals, ++i) {
Chris Lattnerc3a9f8d2006-02-23 19:21:04 +0000313 unsigned Reg = cast<RegisterSDNode>(Node->getOperand(i))->getReg();
314 MI->addMachineRegOperand(Reg, MachineOperand::Use);
315 }
Chris Lattnerdc19b702006-02-04 02:26:14 +0000316 break;
Chris Lattnerc3a9f8d2006-02-23 19:21:04 +0000317 case 2: // Def of register.
Chris Lattnerfd6d2822006-02-24 19:18:20 +0000318 for (; NumVals; --NumVals, ++i) {
Chris Lattnerc3a9f8d2006-02-23 19:21:04 +0000319 unsigned Reg = cast<RegisterSDNode>(Node->getOperand(i))->getReg();
320 MI->addMachineRegOperand(Reg, MachineOperand::Def);
321 }
Chris Lattnerdc19b702006-02-04 02:26:14 +0000322 break;
Chris Lattnerdc19b702006-02-04 02:26:14 +0000323 case 3: { // Immediate.
Chris Lattnerfd6d2822006-02-24 19:18:20 +0000324 assert(NumVals == 1 && "Unknown immediate value!");
Chris Lattnerdc19b702006-02-04 02:26:14 +0000325 uint64_t Val = cast<ConstantSDNode>(Node->getOperand(i))->getValue();
326 MI->addZeroExtImm64Operand(Val);
Chris Lattnerc3a9f8d2006-02-23 19:21:04 +0000327 ++i;
Chris Lattnerdc19b702006-02-04 02:26:14 +0000328 break;
329 }
Chris Lattnerfd6d2822006-02-24 19:18:20 +0000330 case 4: // Addressing mode.
331 // The addressing mode has been selected, just add all of the
332 // operands to the machine instruction.
333 for (; NumVals; --NumVals, ++i)
Chris Lattnerdf375062006-03-10 07:25:12 +0000334 AddOperand(MI, Node->getOperand(i), 0, 0, VRBaseMap);
Chris Lattnerfd6d2822006-02-24 19:18:20 +0000335 break;
Chris Lattnerdc19b702006-02-04 02:26:14 +0000336 }
Chris Lattneracc43bf2006-01-26 23:28:04 +0000337 }
338 break;
339 }
Jim Laskeyb6d4c2c2005-09-30 19:15:27 +0000340 }
341 }
342
Chris Lattnerdf375062006-03-10 07:25:12 +0000343 assert(!VRBaseMap.count(Node) && "Node emitted out of order - early");
344 VRBaseMap[Node] = VRBase;
Jim Laskeyb6d4c2c2005-09-30 19:15:27 +0000345}
346
Chris Lattnera93dfcd2006-03-05 23:51:47 +0000347void ScheduleDAG::EmitNoop() {
348 TII->insertNoop(*BB, BB->end());
349}
350
Evan Chenga9c20912006-01-21 02:32:06 +0000351/// Run - perform scheduling.
352///
353MachineBasicBlock *ScheduleDAG::Run() {
354 TII = TM.getInstrInfo();
355 MRI = TM.getRegisterInfo();
356 RegMap = BB->getParent()->getSSARegMap();
357 ConstPool = BB->getParent()->getConstantPool();
Evan Cheng4ef10862006-01-23 07:01:07 +0000358
Evan Chenga9c20912006-01-21 02:32:06 +0000359 Schedule();
360 return BB;
Chris Lattnerd32b2362005-08-18 18:45:24 +0000361}
Evan Cheng4ef10862006-01-23 07:01:07 +0000362
363