blob: b7d70e419d558aaf295f27e8bae933a53952fb24 [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 Lattnerd32b2362005-08-18 18:45:24 +000023using namespace llvm;
24
Jim Laskeye6b90fb2005-09-26 21:57:04 +000025
26/// CountResults - The results of target nodes have register or immediate
27/// operands first, then an optional chain, and optional flag operands (which do
28/// not go into the machine instrs.)
Evan Chenga9c20912006-01-21 02:32:06 +000029static unsigned CountResults(SDNode *Node) {
Jim Laskeyb6d4c2c2005-09-30 19:15:27 +000030 unsigned N = Node->getNumValues();
31 while (N && Node->getValueType(N - 1) == MVT::Flag)
Jim Laskeye6b90fb2005-09-26 21:57:04 +000032 --N;
Jim Laskeyb6d4c2c2005-09-30 19:15:27 +000033 if (N && Node->getValueType(N - 1) == MVT::Other)
Jim Laskeye6b90fb2005-09-26 21:57:04 +000034 --N; // Skip over chain result.
35 return N;
36}
37
38/// CountOperands The inputs to target nodes have any actual inputs first,
39/// followed by an optional chain operand, then flag operands. Compute the
40/// number of actual operands that will go into the machine instr.
Evan Chenga9c20912006-01-21 02:32:06 +000041static unsigned CountOperands(SDNode *Node) {
Jim Laskeyb6d4c2c2005-09-30 19:15:27 +000042 unsigned N = Node->getNumOperands();
43 while (N && Node->getOperand(N - 1).getValueType() == MVT::Flag)
Jim Laskeye6b90fb2005-09-26 21:57:04 +000044 --N;
Jim Laskeyb6d4c2c2005-09-30 19:15:27 +000045 if (N && Node->getOperand(N - 1).getValueType() == MVT::Other)
Jim Laskeye6b90fb2005-09-26 21:57:04 +000046 --N; // Ignore chain if it exists.
47 return N;
48}
49
Evan Cheng4ef10862006-01-23 07:01:07 +000050static unsigned CreateVirtualRegisters(MachineInstr *MI,
51 unsigned NumResults,
52 SSARegMap *RegMap,
53 const TargetInstrDescriptor &II) {
Jim Laskeye6b90fb2005-09-26 21:57:04 +000054 // Create the result registers for this node and add the result regs to
55 // the machine instruction.
56 const TargetOperandInfo *OpInfo = II.OpInfo;
57 unsigned ResultReg = RegMap->createVirtualRegister(OpInfo[0].RegClass);
58 MI->addRegOperand(ResultReg, MachineOperand::Def);
59 for (unsigned i = 1; i != NumResults; ++i) {
60 assert(OpInfo[i].RegClass && "Isn't a register operand!");
Chris Lattner505277a2005-10-01 07:45:09 +000061 MI->addRegOperand(RegMap->createVirtualRegister(OpInfo[i].RegClass),
Jim Laskeye6b90fb2005-09-26 21:57:04 +000062 MachineOperand::Def);
63 }
64 return ResultReg;
65}
66
Chris Lattnerdf375062006-03-10 07:25:12 +000067/// getVR - Return the virtual register corresponding to the specified result
68/// of the specified node.
69static unsigned getVR(SDOperand Op, std::map<SDNode*, unsigned> &VRBaseMap) {
70 std::map<SDNode*, unsigned>::iterator I = VRBaseMap.find(Op.Val);
71 assert(I != VRBaseMap.end() && "Node emitted out of order - late");
72 return I->second + Op.ResNo;
73}
74
75
Chris Lattnered18b682006-02-24 18:54:03 +000076/// AddOperand - Add the specified operand to the specified machine instr. II
77/// specifies the instruction information for the node, and IIOpNum is the
78/// operand number (in the II) that we are adding. IIOpNum and II are used for
79/// assertions only.
80void ScheduleDAG::AddOperand(MachineInstr *MI, SDOperand Op,
81 unsigned IIOpNum,
Chris Lattnerdf375062006-03-10 07:25:12 +000082 const TargetInstrDescriptor *II,
83 std::map<SDNode*, unsigned> &VRBaseMap) {
Chris Lattnered18b682006-02-24 18:54:03 +000084 if (Op.isTargetOpcode()) {
85 // Note that this case is redundant with the final else block, but we
86 // include it because it is the most common and it makes the logic
87 // simpler here.
88 assert(Op.getValueType() != MVT::Other &&
89 Op.getValueType() != MVT::Flag &&
90 "Chain and flag operands should occur at end of operand list!");
91
92 // Get/emit the operand.
Chris Lattnerdf375062006-03-10 07:25:12 +000093 unsigned VReg = getVR(Op, VRBaseMap);
Chris Lattnered18b682006-02-24 18:54:03 +000094 MI->addRegOperand(VReg, MachineOperand::Use);
95
96 // Verify that it is right.
97 assert(MRegisterInfo::isVirtualRegister(VReg) && "Not a vreg?");
98 if (II) {
99 assert(II->OpInfo[IIOpNum].RegClass &&
100 "Don't have operand info for this instruction!");
101 assert(RegMap->getRegClass(VReg) == II->OpInfo[IIOpNum].RegClass &&
102 "Register class of operand and regclass of use don't agree!");
103 }
104 } else if (ConstantSDNode *C =
105 dyn_cast<ConstantSDNode>(Op)) {
106 MI->addZeroExtImm64Operand(C->getValue());
107 } else if (RegisterSDNode*R =
108 dyn_cast<RegisterSDNode>(Op)) {
109 MI->addRegOperand(R->getReg(), MachineOperand::Use);
110 } else if (GlobalAddressSDNode *TGA =
111 dyn_cast<GlobalAddressSDNode>(Op)) {
112 MI->addGlobalAddressOperand(TGA->getGlobal(), false, TGA->getOffset());
113 } else if (BasicBlockSDNode *BB =
114 dyn_cast<BasicBlockSDNode>(Op)) {
115 MI->addMachineBasicBlockOperand(BB->getBasicBlock());
116 } else if (FrameIndexSDNode *FI =
117 dyn_cast<FrameIndexSDNode>(Op)) {
118 MI->addFrameIndexOperand(FI->getIndex());
119 } else if (ConstantPoolSDNode *CP =
120 dyn_cast<ConstantPoolSDNode>(Op)) {
Evan Cheng404cb4f2006-02-25 09:54:52 +0000121 int Offset = CP->getOffset();
Chris Lattnered18b682006-02-24 18:54:03 +0000122 unsigned Align = CP->getAlignment();
123 // MachineConstantPool wants an explicit alignment.
124 if (Align == 0) {
125 if (CP->get()->getType() == Type::DoubleTy)
126 Align = 3; // always 8-byte align doubles.
127 else
128 Align = TM.getTargetData()
129 .getTypeAlignmentShift(CP->get()->getType());
130 }
131
132 unsigned Idx = ConstPool->getConstantPoolIndex(CP->get(), Align);
Evan Cheng404cb4f2006-02-25 09:54:52 +0000133 MI->addConstantPoolIndexOperand(Idx, Offset);
Chris Lattnered18b682006-02-24 18:54:03 +0000134 } else if (ExternalSymbolSDNode *ES =
135 dyn_cast<ExternalSymbolSDNode>(Op)) {
136 MI->addExternalSymbolOperand(ES->getSymbol(), false);
137 } else {
138 assert(Op.getValueType() != MVT::Other &&
139 Op.getValueType() != MVT::Flag &&
140 "Chain and flag operands should occur at end of operand list!");
Chris Lattnerdf375062006-03-10 07:25:12 +0000141 unsigned VReg = getVR(Op, VRBaseMap);
Chris Lattnered18b682006-02-24 18:54:03 +0000142 MI->addRegOperand(VReg, MachineOperand::Use);
143
144 // Verify that it is right.
145 assert(MRegisterInfo::isVirtualRegister(VReg) && "Not a vreg?");
146 if (II) {
147 assert(II->OpInfo[IIOpNum].RegClass &&
148 "Don't have operand info for this instruction!");
149 assert(RegMap->getRegClass(VReg) == II->OpInfo[IIOpNum].RegClass &&
150 "Register class of operand and regclass of use don't agree!");
151 }
152 }
153
154}
155
156
Jim Laskeyb6d4c2c2005-09-30 19:15:27 +0000157/// EmitNode - Generate machine code for an node and needed dependencies.
Jim Laskeye6b90fb2005-09-26 21:57:04 +0000158///
Chris Lattner8c7ef052006-03-10 07:28:36 +0000159void ScheduleDAG::EmitNode(SDNode *Node,
Chris Lattnerdf375062006-03-10 07:25:12 +0000160 std::map<SDNode*, unsigned> &VRBaseMap) {
Jim Laskeyb6d4c2c2005-09-30 19:15:27 +0000161 unsigned VRBase = 0; // First virtual register for node
Chris Lattner2d973e42005-08-18 20:07:59 +0000162
Jim Laskeyb6d4c2c2005-09-30 19:15:27 +0000163 // If machine instruction
164 if (Node->isTargetOpcode()) {
165 unsigned Opc = Node->getTargetOpcode();
Evan Chenga9c20912006-01-21 02:32:06 +0000166 const TargetInstrDescriptor &II = TII->get(Opc);
Chris Lattner2d973e42005-08-18 20:07:59 +0000167
Jim Laskeyb6d4c2c2005-09-30 19:15:27 +0000168 unsigned NumResults = CountResults(Node);
169 unsigned NodeOperands = CountOperands(Node);
Jim Laskeye6b90fb2005-09-26 21:57:04 +0000170 unsigned NumMIOperands = NodeOperands + NumResults;
Chris Lattnerda8abb02005-09-01 18:44:10 +0000171#ifndef NDEBUG
Chris Lattner14b392a2005-08-24 22:02:41 +0000172 assert((unsigned(II.numOperands) == NumMIOperands || II.numOperands == -1)&&
Chris Lattner2d973e42005-08-18 20:07:59 +0000173 "#operands for dag node doesn't match .td file!");
Chris Lattnerca6aa2f2005-08-19 01:01:34 +0000174#endif
Chris Lattner2d973e42005-08-18 20:07:59 +0000175
176 // Create the new machine instruction.
Chris Lattner14b392a2005-08-24 22:02:41 +0000177 MachineInstr *MI = new MachineInstr(Opc, NumMIOperands, true, true);
Chris Lattner2d973e42005-08-18 20:07:59 +0000178
179 // Add result register values for things that are defined by this
180 // instruction.
Chris Lattnera4176522005-10-30 18:54:27 +0000181
182 // If the node is only used by a CopyToReg and the dest reg is a vreg, use
183 // the CopyToReg'd destination register instead of creating a new vreg.
184 if (NumResults == 1) {
185 for (SDNode::use_iterator UI = Node->use_begin(), E = Node->use_end();
186 UI != E; ++UI) {
187 SDNode *Use = *UI;
188 if (Use->getOpcode() == ISD::CopyToReg &&
189 Use->getOperand(2).Val == Node) {
190 unsigned Reg = cast<RegisterSDNode>(Use->getOperand(1))->getReg();
191 if (MRegisterInfo::isVirtualRegister(Reg)) {
192 VRBase = Reg;
193 MI->addRegOperand(Reg, MachineOperand::Def);
194 break;
195 }
196 }
197 }
198 }
199
200 // Otherwise, create new virtual registers.
201 if (NumResults && VRBase == 0)
Evan Cheng4ef10862006-01-23 07:01:07 +0000202 VRBase = CreateVirtualRegisters(MI, NumResults, RegMap, II);
Jim Laskeyb6d4c2c2005-09-30 19:15:27 +0000203
204 // Emit all of the actual operands of this instruction, adding them to the
205 // instruction as appropriate.
Chris Lattnered18b682006-02-24 18:54:03 +0000206 for (unsigned i = 0; i != NodeOperands; ++i)
Chris Lattnerdf375062006-03-10 07:25:12 +0000207 AddOperand(MI, Node->getOperand(i), i+NumResults, &II, VRBaseMap);
Jim Laskeyb6d4c2c2005-09-30 19:15:27 +0000208
209 // Now that we have emitted all operands, emit this instruction itself.
210 if ((II.Flags & M_USES_CUSTOM_DAG_SCHED_INSERTION) == 0) {
211 BB->insert(BB->end(), MI);
212 } else {
213 // Insert this instruction into the end of the basic block, potentially
214 // taking some custom action.
215 BB = DAG.getTargetLoweringInfo().InsertAtEndOfBasicBlock(MI, BB);
216 }
217 } else {
218 switch (Node->getOpcode()) {
219 default:
220 Node->dump();
221 assert(0 && "This target-independent node should have been selected!");
222 case ISD::EntryToken: // fall thru
223 case ISD::TokenFactor:
224 break;
225 case ISD::CopyToReg: {
Chris Lattnerdf375062006-03-10 07:25:12 +0000226 unsigned InReg = getVR(Node->getOperand(2), VRBaseMap);
Chris Lattnera4176522005-10-30 18:54:27 +0000227 unsigned DestReg = cast<RegisterSDNode>(Node->getOperand(1))->getReg();
228 if (InReg != DestReg) // Coallesced away the copy?
Evan Chenga9c20912006-01-21 02:32:06 +0000229 MRI->copyRegToReg(*BB, BB->end(), DestReg, InReg,
230 RegMap->getRegClass(InReg));
Jim Laskeyb6d4c2c2005-09-30 19:15:27 +0000231 break;
232 }
233 case ISD::CopyFromReg: {
234 unsigned SrcReg = cast<RegisterSDNode>(Node->getOperand(1))->getReg();
Chris Lattner089c25c2005-10-09 05:58:56 +0000235 if (MRegisterInfo::isVirtualRegister(SrcReg)) {
236 VRBase = SrcReg; // Just use the input register directly!
237 break;
238 }
239
Chris Lattnera4176522005-10-30 18:54:27 +0000240 // If the node is only used by a CopyToReg and the dest reg is a vreg, use
241 // the CopyToReg'd destination register instead of creating a new vreg.
242 for (SDNode::use_iterator UI = Node->use_begin(), E = Node->use_end();
243 UI != E; ++UI) {
244 SDNode *Use = *UI;
245 if (Use->getOpcode() == ISD::CopyToReg &&
246 Use->getOperand(2).Val == Node) {
247 unsigned DestReg = cast<RegisterSDNode>(Use->getOperand(1))->getReg();
248 if (MRegisterInfo::isVirtualRegister(DestReg)) {
249 VRBase = DestReg;
250 break;
251 }
252 }
253 }
254
Jim Laskeyb6d4c2c2005-09-30 19:15:27 +0000255 // Figure out the register class to create for the destreg.
256 const TargetRegisterClass *TRC = 0;
Chris Lattnera4176522005-10-30 18:54:27 +0000257 if (VRBase) {
258 TRC = RegMap->getRegClass(VRBase);
259 } else {
Chris Lattner089c25c2005-10-09 05:58:56 +0000260
Chris Lattnera4176522005-10-30 18:54:27 +0000261 // Pick the register class of the right type that contains this physreg.
Evan Chenga9c20912006-01-21 02:32:06 +0000262 for (MRegisterInfo::regclass_iterator I = MRI->regclass_begin(),
263 E = MRI->regclass_end(); I != E; ++I)
Nate Begeman6510b222005-12-01 04:51:06 +0000264 if ((*I)->hasType(Node->getValueType(0)) &&
Chris Lattnera4176522005-10-30 18:54:27 +0000265 (*I)->contains(SrcReg)) {
266 TRC = *I;
267 break;
268 }
269 assert(TRC && "Couldn't find register class for reg copy!");
Jim Laskeyb6d4c2c2005-09-30 19:15:27 +0000270
Chris Lattnera4176522005-10-30 18:54:27 +0000271 // Create the reg, emit the copy.
272 VRBase = RegMap->createVirtualRegister(TRC);
273 }
Evan Chenga9c20912006-01-21 02:32:06 +0000274 MRI->copyRegToReg(*BB, BB->end(), VRBase, SrcReg, TRC);
Jim Laskeyb6d4c2c2005-09-30 19:15:27 +0000275 break;
276 }
Chris Lattneracc43bf2006-01-26 23:28:04 +0000277 case ISD::INLINEASM: {
278 unsigned NumOps = Node->getNumOperands();
279 if (Node->getOperand(NumOps-1).getValueType() == MVT::Flag)
280 --NumOps; // Ignore the flag operand.
281
282 // Create the inline asm machine instruction.
283 MachineInstr *MI =
284 new MachineInstr(BB, TargetInstrInfo::INLINEASM, (NumOps-2)/2+1);
285
286 // Add the asm string as an external symbol operand.
287 const char *AsmStr =
288 cast<ExternalSymbolSDNode>(Node->getOperand(1))->getSymbol();
289 MI->addExternalSymbolOperand(AsmStr, false);
290
291 // Add all of the operand registers to the instruction.
Chris Lattnerc3a9f8d2006-02-23 19:21:04 +0000292 for (unsigned i = 2; i != NumOps;) {
293 unsigned Flags = cast<ConstantSDNode>(Node->getOperand(i))->getValue();
Chris Lattnerfd6d2822006-02-24 19:18:20 +0000294 unsigned NumVals = Flags >> 3;
Chris Lattnerc3a9f8d2006-02-23 19:21:04 +0000295
Chris Lattnerdaf6bc62006-02-24 19:50:58 +0000296 MI->addZeroExtImm64Operand(Flags);
Chris Lattnerc3a9f8d2006-02-23 19:21:04 +0000297 ++i; // Skip the ID value.
298
299 switch (Flags & 7) {
Chris Lattneracc43bf2006-01-26 23:28:04 +0000300 default: assert(0 && "Bad flags!");
Chris Lattnerc3a9f8d2006-02-23 19:21:04 +0000301 case 1: // Use of register.
Chris Lattnerfd6d2822006-02-24 19:18:20 +0000302 for (; NumVals; --NumVals, ++i) {
Chris Lattnerc3a9f8d2006-02-23 19:21:04 +0000303 unsigned Reg = cast<RegisterSDNode>(Node->getOperand(i))->getReg();
304 MI->addMachineRegOperand(Reg, MachineOperand::Use);
305 }
Chris Lattnerdc19b702006-02-04 02:26:14 +0000306 break;
Chris Lattnerc3a9f8d2006-02-23 19:21:04 +0000307 case 2: // Def of register.
Chris Lattnerfd6d2822006-02-24 19:18:20 +0000308 for (; NumVals; --NumVals, ++i) {
Chris Lattnerc3a9f8d2006-02-23 19:21:04 +0000309 unsigned Reg = cast<RegisterSDNode>(Node->getOperand(i))->getReg();
310 MI->addMachineRegOperand(Reg, MachineOperand::Def);
311 }
Chris Lattnerdc19b702006-02-04 02:26:14 +0000312 break;
Chris Lattnerdc19b702006-02-04 02:26:14 +0000313 case 3: { // Immediate.
Chris Lattnerfd6d2822006-02-24 19:18:20 +0000314 assert(NumVals == 1 && "Unknown immediate value!");
Chris Lattnerdc19b702006-02-04 02:26:14 +0000315 uint64_t Val = cast<ConstantSDNode>(Node->getOperand(i))->getValue();
316 MI->addZeroExtImm64Operand(Val);
Chris Lattnerc3a9f8d2006-02-23 19:21:04 +0000317 ++i;
Chris Lattnerdc19b702006-02-04 02:26:14 +0000318 break;
319 }
Chris Lattnerfd6d2822006-02-24 19:18:20 +0000320 case 4: // Addressing mode.
321 // The addressing mode has been selected, just add all of the
322 // operands to the machine instruction.
323 for (; NumVals; --NumVals, ++i)
Chris Lattnerdf375062006-03-10 07:25:12 +0000324 AddOperand(MI, Node->getOperand(i), 0, 0, VRBaseMap);
Chris Lattnerfd6d2822006-02-24 19:18:20 +0000325 break;
Chris Lattnerdc19b702006-02-04 02:26:14 +0000326 }
Chris Lattneracc43bf2006-01-26 23:28:04 +0000327 }
328 break;
329 }
Jim Laskeyb6d4c2c2005-09-30 19:15:27 +0000330 }
331 }
332
Chris Lattnerdf375062006-03-10 07:25:12 +0000333 assert(!VRBaseMap.count(Node) && "Node emitted out of order - early");
334 VRBaseMap[Node] = VRBase;
Jim Laskeyb6d4c2c2005-09-30 19:15:27 +0000335}
336
Chris Lattnera93dfcd2006-03-05 23:51:47 +0000337void ScheduleDAG::EmitNoop() {
338 TII->insertNoop(*BB, BB->end());
339}
340
Evan Chenga9c20912006-01-21 02:32:06 +0000341/// Run - perform scheduling.
342///
343MachineBasicBlock *ScheduleDAG::Run() {
344 TII = TM.getInstrInfo();
345 MRI = TM.getRegisterInfo();
346 RegMap = BB->getParent()->getSSARegMap();
347 ConstPool = BB->getParent()->getConstantPool();
Evan Cheng4ef10862006-01-23 07:01:07 +0000348
Evan Chenga9c20912006-01-21 02:32:06 +0000349 Schedule();
350 return BB;
Chris Lattnerd32b2362005-08-18 18:45:24 +0000351}
Evan Cheng4ef10862006-01-23 07:01:07 +0000352
353