blob: 9c1f1a8d63627f12fa53276e6339898392677dc5 [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
16#define DEBUG_TYPE "sched"
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"
Evan Chenga9c20912006-01-21 02:32:06 +000019#include "llvm/CodeGen/ScheduleDAG.h"
Chris Lattner4ccd4062005-08-19 20:45:43 +000020#include "llvm/CodeGen/SSARegMap.h"
Chris Lattner2d973e42005-08-18 20:07:59 +000021#include "llvm/Target/TargetMachine.h"
22#include "llvm/Target/TargetInstrInfo.h"
Jim Laskey7d090f32005-11-04 04:05:35 +000023#include "llvm/Target/TargetInstrItineraries.h"
Chris Lattner025c39b2005-08-26 20:54:47 +000024#include "llvm/Target/TargetLowering.h"
Jim Laskeye6b90fb2005-09-26 21:57:04 +000025#include "llvm/Support/Debug.h"
26#include <iostream>
Chris Lattnerd32b2362005-08-18 18:45:24 +000027using namespace llvm;
28
Jim Laskeye6b90fb2005-09-26 21:57:04 +000029
30/// CountResults - The results of target nodes have register or immediate
31/// operands first, then an optional chain, and optional flag operands (which do
32/// not go into the machine instrs.)
Evan Chenga9c20912006-01-21 02:32:06 +000033static unsigned CountResults(SDNode *Node) {
Jim Laskeyb6d4c2c2005-09-30 19:15:27 +000034 unsigned N = Node->getNumValues();
35 while (N && Node->getValueType(N - 1) == MVT::Flag)
Jim Laskeye6b90fb2005-09-26 21:57:04 +000036 --N;
Jim Laskeyb6d4c2c2005-09-30 19:15:27 +000037 if (N && Node->getValueType(N - 1) == MVT::Other)
Jim Laskeye6b90fb2005-09-26 21:57:04 +000038 --N; // Skip over chain result.
39 return N;
40}
41
42/// CountOperands The inputs to target nodes have any actual inputs first,
43/// followed by an optional chain operand, then flag operands. Compute the
44/// number of actual operands that will go into the machine instr.
Evan Chenga9c20912006-01-21 02:32:06 +000045static unsigned CountOperands(SDNode *Node) {
Jim Laskeyb6d4c2c2005-09-30 19:15:27 +000046 unsigned N = Node->getNumOperands();
47 while (N && Node->getOperand(N - 1).getValueType() == MVT::Flag)
Jim Laskeye6b90fb2005-09-26 21:57:04 +000048 --N;
Jim Laskeyb6d4c2c2005-09-30 19:15:27 +000049 if (N && Node->getOperand(N - 1).getValueType() == MVT::Other)
Jim Laskeye6b90fb2005-09-26 21:57:04 +000050 --N; // Ignore chain if it exists.
51 return N;
52}
53
54/// CreateVirtualRegisters - Add result register values for things that are
55/// defined by this instruction.
Evan Chenga9c20912006-01-21 02:32:06 +000056unsigned ScheduleDAG::CreateVirtualRegisters(MachineInstr *MI,
Jim Laskeye6b90fb2005-09-26 21:57:04 +000057 unsigned NumResults,
58 const TargetInstrDescriptor &II) {
59 // Create the result registers for this node and add the result regs to
60 // the machine instruction.
61 const TargetOperandInfo *OpInfo = II.OpInfo;
62 unsigned ResultReg = RegMap->createVirtualRegister(OpInfo[0].RegClass);
63 MI->addRegOperand(ResultReg, MachineOperand::Def);
64 for (unsigned i = 1; i != NumResults; ++i) {
65 assert(OpInfo[i].RegClass && "Isn't a register operand!");
Chris Lattner505277a2005-10-01 07:45:09 +000066 MI->addRegOperand(RegMap->createVirtualRegister(OpInfo[i].RegClass),
Jim Laskeye6b90fb2005-09-26 21:57:04 +000067 MachineOperand::Def);
68 }
69 return ResultReg;
70}
71
Jim Laskeyb6d4c2c2005-09-30 19:15:27 +000072/// EmitNode - Generate machine code for an node and needed dependencies.
Jim Laskeye6b90fb2005-09-26 21:57:04 +000073///
Evan Chenga9c20912006-01-21 02:32:06 +000074void ScheduleDAG::EmitNode(NodeInfo *NI) {
Jim Laskeyb6d4c2c2005-09-30 19:15:27 +000075 unsigned VRBase = 0; // First virtual register for node
76 SDNode *Node = NI->Node;
Chris Lattner2d973e42005-08-18 20:07:59 +000077
Jim Laskeyb6d4c2c2005-09-30 19:15:27 +000078 // If machine instruction
79 if (Node->isTargetOpcode()) {
80 unsigned Opc = Node->getTargetOpcode();
Evan Chenga9c20912006-01-21 02:32:06 +000081 const TargetInstrDescriptor &II = TII->get(Opc);
Chris Lattner2d973e42005-08-18 20:07:59 +000082
Jim Laskeyb6d4c2c2005-09-30 19:15:27 +000083 unsigned NumResults = CountResults(Node);
84 unsigned NodeOperands = CountOperands(Node);
Jim Laskeye6b90fb2005-09-26 21:57:04 +000085 unsigned NumMIOperands = NodeOperands + NumResults;
Chris Lattnerda8abb02005-09-01 18:44:10 +000086#ifndef NDEBUG
Chris Lattner14b392a2005-08-24 22:02:41 +000087 assert((unsigned(II.numOperands) == NumMIOperands || II.numOperands == -1)&&
Chris Lattner2d973e42005-08-18 20:07:59 +000088 "#operands for dag node doesn't match .td file!");
Chris Lattnerca6aa2f2005-08-19 01:01:34 +000089#endif
Chris Lattner2d973e42005-08-18 20:07:59 +000090
91 // Create the new machine instruction.
Chris Lattner14b392a2005-08-24 22:02:41 +000092 MachineInstr *MI = new MachineInstr(Opc, NumMIOperands, true, true);
Chris Lattner2d973e42005-08-18 20:07:59 +000093
94 // Add result register values for things that are defined by this
95 // instruction.
Chris Lattnera4176522005-10-30 18:54:27 +000096
97 // If the node is only used by a CopyToReg and the dest reg is a vreg, use
98 // the CopyToReg'd destination register instead of creating a new vreg.
99 if (NumResults == 1) {
100 for (SDNode::use_iterator UI = Node->use_begin(), E = Node->use_end();
101 UI != E; ++UI) {
102 SDNode *Use = *UI;
103 if (Use->getOpcode() == ISD::CopyToReg &&
104 Use->getOperand(2).Val == Node) {
105 unsigned Reg = cast<RegisterSDNode>(Use->getOperand(1))->getReg();
106 if (MRegisterInfo::isVirtualRegister(Reg)) {
107 VRBase = Reg;
108 MI->addRegOperand(Reg, MachineOperand::Def);
109 break;
110 }
111 }
112 }
113 }
114
115 // Otherwise, create new virtual registers.
116 if (NumResults && VRBase == 0)
117 VRBase = CreateVirtualRegisters(MI, NumResults, II);
Jim Laskeyb6d4c2c2005-09-30 19:15:27 +0000118
119 // Emit all of the actual operands of this instruction, adding them to the
120 // instruction as appropriate.
121 for (unsigned i = 0; i != NodeOperands; ++i) {
122 if (Node->getOperand(i).isTargetOpcode()) {
123 // Note that this case is redundant with the final else block, but we
124 // include it because it is the most common and it makes the logic
125 // simpler here.
126 assert(Node->getOperand(i).getValueType() != MVT::Other &&
127 Node->getOperand(i).getValueType() != MVT::Flag &&
128 "Chain and flag operands should occur at end of operand list!");
Chris Lattner505277a2005-10-01 07:45:09 +0000129
130 // Get/emit the operand.
131 unsigned VReg = getVR(Node->getOperand(i));
132 MI->addRegOperand(VReg, MachineOperand::Use);
Jim Laskeyb6d4c2c2005-09-30 19:15:27 +0000133
Chris Lattner505277a2005-10-01 07:45:09 +0000134 // Verify that it is right.
135 assert(MRegisterInfo::isVirtualRegister(VReg) && "Not a vreg?");
136 assert(II.OpInfo[i+NumResults].RegClass &&
137 "Don't have operand info for this instruction!");
138 assert(RegMap->getRegClass(VReg) == II.OpInfo[i+NumResults].RegClass &&
139 "Register class of operand and regclass of use don't agree!");
Jim Laskeyb6d4c2c2005-09-30 19:15:27 +0000140 } else if (ConstantSDNode *C =
141 dyn_cast<ConstantSDNode>(Node->getOperand(i))) {
142 MI->addZeroExtImm64Operand(C->getValue());
143 } else if (RegisterSDNode*R =
144 dyn_cast<RegisterSDNode>(Node->getOperand(i))) {
145 MI->addRegOperand(R->getReg(), MachineOperand::Use);
146 } else if (GlobalAddressSDNode *TGA =
147 dyn_cast<GlobalAddressSDNode>(Node->getOperand(i))) {
Evan Cheng61ca74b2005-11-30 02:04:11 +0000148 MI->addGlobalAddressOperand(TGA->getGlobal(), false, TGA->getOffset());
Jim Laskeyb6d4c2c2005-09-30 19:15:27 +0000149 } else if (BasicBlockSDNode *BB =
150 dyn_cast<BasicBlockSDNode>(Node->getOperand(i))) {
151 MI->addMachineBasicBlockOperand(BB->getBasicBlock());
152 } else if (FrameIndexSDNode *FI =
153 dyn_cast<FrameIndexSDNode>(Node->getOperand(i))) {
154 MI->addFrameIndexOperand(FI->getIndex());
155 } else if (ConstantPoolSDNode *CP =
156 dyn_cast<ConstantPoolSDNode>(Node->getOperand(i))) {
157 unsigned Idx = ConstPool->getConstantPoolIndex(CP->get());
158 MI->addConstantPoolIndexOperand(Idx);
159 } else if (ExternalSymbolSDNode *ES =
160 dyn_cast<ExternalSymbolSDNode>(Node->getOperand(i))) {
161 MI->addExternalSymbolOperand(ES->getSymbol(), false);
162 } else {
163 assert(Node->getOperand(i).getValueType() != MVT::Other &&
164 Node->getOperand(i).getValueType() != MVT::Flag &&
165 "Chain and flag operands should occur at end of operand list!");
Chris Lattner505277a2005-10-01 07:45:09 +0000166 unsigned VReg = getVR(Node->getOperand(i));
167 MI->addRegOperand(VReg, MachineOperand::Use);
168
169 // Verify that it is right.
170 assert(MRegisterInfo::isVirtualRegister(VReg) && "Not a vreg?");
171 assert(II.OpInfo[i+NumResults].RegClass &&
172 "Don't have operand info for this instruction!");
173 assert(RegMap->getRegClass(VReg) == II.OpInfo[i+NumResults].RegClass &&
174 "Register class of operand and regclass of use don't agree!");
Jim Laskeyb6d4c2c2005-09-30 19:15:27 +0000175 }
176 }
177
178 // Now that we have emitted all operands, emit this instruction itself.
179 if ((II.Flags & M_USES_CUSTOM_DAG_SCHED_INSERTION) == 0) {
180 BB->insert(BB->end(), MI);
181 } else {
182 // Insert this instruction into the end of the basic block, potentially
183 // taking some custom action.
184 BB = DAG.getTargetLoweringInfo().InsertAtEndOfBasicBlock(MI, BB);
185 }
186 } else {
187 switch (Node->getOpcode()) {
188 default:
189 Node->dump();
190 assert(0 && "This target-independent node should have been selected!");
191 case ISD::EntryToken: // fall thru
192 case ISD::TokenFactor:
193 break;
194 case ISD::CopyToReg: {
Chris Lattnera4176522005-10-30 18:54:27 +0000195 unsigned InReg = getVR(Node->getOperand(2));
196 unsigned DestReg = cast<RegisterSDNode>(Node->getOperand(1))->getReg();
197 if (InReg != DestReg) // Coallesced away the copy?
Evan Chenga9c20912006-01-21 02:32:06 +0000198 MRI->copyRegToReg(*BB, BB->end(), DestReg, InReg,
199 RegMap->getRegClass(InReg));
Jim Laskeyb6d4c2c2005-09-30 19:15:27 +0000200 break;
201 }
202 case ISD::CopyFromReg: {
203 unsigned SrcReg = cast<RegisterSDNode>(Node->getOperand(1))->getReg();
Chris Lattner089c25c2005-10-09 05:58:56 +0000204 if (MRegisterInfo::isVirtualRegister(SrcReg)) {
205 VRBase = SrcReg; // Just use the input register directly!
206 break;
207 }
208
Chris Lattnera4176522005-10-30 18:54:27 +0000209 // If the node is only used by a CopyToReg and the dest reg is a vreg, use
210 // the CopyToReg'd destination register instead of creating a new vreg.
211 for (SDNode::use_iterator UI = Node->use_begin(), E = Node->use_end();
212 UI != E; ++UI) {
213 SDNode *Use = *UI;
214 if (Use->getOpcode() == ISD::CopyToReg &&
215 Use->getOperand(2).Val == Node) {
216 unsigned DestReg = cast<RegisterSDNode>(Use->getOperand(1))->getReg();
217 if (MRegisterInfo::isVirtualRegister(DestReg)) {
218 VRBase = DestReg;
219 break;
220 }
221 }
222 }
223
Jim Laskeyb6d4c2c2005-09-30 19:15:27 +0000224 // Figure out the register class to create for the destreg.
225 const TargetRegisterClass *TRC = 0;
Chris Lattnera4176522005-10-30 18:54:27 +0000226 if (VRBase) {
227 TRC = RegMap->getRegClass(VRBase);
228 } else {
Chris Lattner089c25c2005-10-09 05:58:56 +0000229
Chris Lattnera4176522005-10-30 18:54:27 +0000230 // Pick the register class of the right type that contains this physreg.
Evan Chenga9c20912006-01-21 02:32:06 +0000231 for (MRegisterInfo::regclass_iterator I = MRI->regclass_begin(),
232 E = MRI->regclass_end(); I != E; ++I)
Nate Begeman6510b222005-12-01 04:51:06 +0000233 if ((*I)->hasType(Node->getValueType(0)) &&
Chris Lattnera4176522005-10-30 18:54:27 +0000234 (*I)->contains(SrcReg)) {
235 TRC = *I;
236 break;
237 }
238 assert(TRC && "Couldn't find register class for reg copy!");
Jim Laskeyb6d4c2c2005-09-30 19:15:27 +0000239
Chris Lattnera4176522005-10-30 18:54:27 +0000240 // Create the reg, emit the copy.
241 VRBase = RegMap->createVirtualRegister(TRC);
242 }
Evan Chenga9c20912006-01-21 02:32:06 +0000243 MRI->copyRegToReg(*BB, BB->end(), VRBase, SrcReg, TRC);
Jim Laskeyb6d4c2c2005-09-30 19:15:27 +0000244 break;
245 }
246 }
247 }
248
249 assert(NI->VRBase == 0 && "Node emitted out of order - early");
250 NI->VRBase = VRBase;
251}
252
Evan Chenga9c20912006-01-21 02:32:06 +0000253void ScheduleDAG::dump(const char *tag) const {
254 std::cerr << tag; dump();
Jim Laskeyfab66f62005-10-12 18:29:35 +0000255}
256
Evan Chenga9c20912006-01-21 02:32:06 +0000257void ScheduleDAG::dump() const {
Jim Laskeye6b90fb2005-09-26 21:57:04 +0000258 print(std::cerr);
259}
Jim Laskeye6b90fb2005-09-26 21:57:04 +0000260
Evan Chenga9c20912006-01-21 02:32:06 +0000261/// Run - perform scheduling.
262///
263MachineBasicBlock *ScheduleDAG::Run() {
264 TII = TM.getInstrInfo();
265 MRI = TM.getRegisterInfo();
266 RegMap = BB->getParent()->getSSARegMap();
267 ConstPool = BB->getParent()->getConstantPool();
268 Schedule();
269 return BB;
Chris Lattnerd32b2362005-08-18 18:45:24 +0000270}