1 //===---- ScheduleDAG.cpp - Implement the ScheduleDAG class ---------------===//
3 // The LLVM Compiler Infrastructure
5 // This file was developed by James M. Laskey and is distributed under the
6 // University of Illinois Open Source License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // 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.
14 //===----------------------------------------------------------------------===//
16 #define DEBUG_TYPE "sched"
17 #include "llvm/CodeGen/MachineConstantPool.h"
18 #include "llvm/CodeGen/MachineFunction.h"
19 #include "llvm/CodeGen/ScheduleDAG.h"
20 #include "llvm/CodeGen/SSARegMap.h"
21 #include "llvm/Target/TargetMachine.h"
22 #include "llvm/Target/TargetInstrInfo.h"
23 #include "llvm/Target/TargetInstrItineraries.h"
24 #include "llvm/Target/TargetLowering.h"
25 #include "llvm/Support/Debug.h"
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.)
33 static unsigned CountResults(SDNode *Node) {
34 unsigned N = Node->getNumValues();
35 while (N && Node->getValueType(N - 1) == MVT::Flag)
37 if (N && Node->getValueType(N - 1) == MVT::Other)
38 --N; // Skip over chain result.
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.
45 static unsigned CountOperands(SDNode *Node) {
46 unsigned N = Node->getNumOperands();
47 while (N && Node->getOperand(N - 1).getValueType() == MVT::Flag)
49 if (N && Node->getOperand(N - 1).getValueType() == MVT::Other)
50 --N; // Ignore chain if it exists.
54 /// CreateVirtualRegisters - Add result register values for things that are
55 /// defined by this instruction.
56 unsigned ScheduleDAG::CreateVirtualRegisters(MachineInstr *MI,
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!");
66 MI->addRegOperand(RegMap->createVirtualRegister(OpInfo[i].RegClass),
72 /// EmitNode - Generate machine code for an node and needed dependencies.
74 void ScheduleDAG::EmitNode(NodeInfo *NI) {
75 unsigned VRBase = 0; // First virtual register for node
76 SDNode *Node = NI->Node;
78 // If machine instruction
79 if (Node->isTargetOpcode()) {
80 unsigned Opc = Node->getTargetOpcode();
81 const TargetInstrDescriptor &II = TII->get(Opc);
83 unsigned NumResults = CountResults(Node);
84 unsigned NodeOperands = CountOperands(Node);
85 unsigned NumMIOperands = NodeOperands + NumResults;
87 assert((unsigned(II.numOperands) == NumMIOperands || II.numOperands == -1)&&
88 "#operands for dag node doesn't match .td file!");
91 // Create the new machine instruction.
92 MachineInstr *MI = new MachineInstr(Opc, NumMIOperands, true, true);
94 // Add result register values for things that are defined by this
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();
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)) {
108 MI->addRegOperand(Reg, MachineOperand::Def);
115 // Otherwise, create new virtual registers.
116 if (NumResults && VRBase == 0)
117 VRBase = CreateVirtualRegisters(MI, NumResults, II);
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
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!");
130 // Get/emit the operand.
131 unsigned VReg = getVR(Node->getOperand(i));
132 MI->addRegOperand(VReg, MachineOperand::Use);
134 // 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!");
140 } 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))) {
148 MI->addGlobalAddressOperand(TGA->getGlobal(), false, TGA->getOffset());
149 } 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);
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!");
166 unsigned VReg = getVR(Node->getOperand(i));
167 MI->addRegOperand(VReg, MachineOperand::Use);
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!");
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);
182 // Insert this instruction into the end of the basic block, potentially
183 // taking some custom action.
184 BB = DAG.getTargetLoweringInfo().InsertAtEndOfBasicBlock(MI, BB);
187 switch (Node->getOpcode()) {
190 assert(0 && "This target-independent node should have been selected!");
191 case ISD::EntryToken: // fall thru
192 case ISD::TokenFactor:
194 case ISD::CopyToReg: {
195 unsigned InReg = getVR(Node->getOperand(2));
196 unsigned DestReg = cast<RegisterSDNode>(Node->getOperand(1))->getReg();
197 if (InReg != DestReg) // Coallesced away the copy?
198 MRI->copyRegToReg(*BB, BB->end(), DestReg, InReg,
199 RegMap->getRegClass(InReg));
202 case ISD::CopyFromReg: {
203 unsigned SrcReg = cast<RegisterSDNode>(Node->getOperand(1))->getReg();
204 if (MRegisterInfo::isVirtualRegister(SrcReg)) {
205 VRBase = SrcReg; // Just use the input register directly!
209 // 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();
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)) {
224 // Figure out the register class to create for the destreg.
225 const TargetRegisterClass *TRC = 0;
227 TRC = RegMap->getRegClass(VRBase);
230 // Pick the register class of the right type that contains this physreg.
231 for (MRegisterInfo::regclass_iterator I = MRI->regclass_begin(),
232 E = MRI->regclass_end(); I != E; ++I)
233 if ((*I)->hasType(Node->getValueType(0)) &&
234 (*I)->contains(SrcReg)) {
238 assert(TRC && "Couldn't find register class for reg copy!");
240 // Create the reg, emit the copy.
241 VRBase = RegMap->createVirtualRegister(TRC);
243 MRI->copyRegToReg(*BB, BB->end(), VRBase, SrcReg, TRC);
249 assert(NI->VRBase == 0 && "Node emitted out of order - early");
253 void ScheduleDAG::dump(const char *tag) const {
254 std::cerr << tag; dump();
257 void ScheduleDAG::dump() const {
261 /// Run - perform scheduling.
263 MachineBasicBlock *ScheduleDAG::Run() {
264 TII = TM.getInstrInfo();
265 MRI = TM.getRegisterInfo();
266 RegMap = BB->getParent()->getSSARegMap();
267 ConstPool = BB->getParent()->getConstantPool();