1 //===-- ScheduleDAG.cpp - Implement a trivial DAG scheduler ---------------===//
3 // The LLVM Compiler Infrastructure
5 // This file was developed by Chris Lattner and is distributed under the
6 // University of Illinois Open Source License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This implements a simple code linearizer for DAGs. This is not a very good
11 // way to emit code, but gets working code quickly.
13 //===----------------------------------------------------------------------===//
15 #define DEBUG_TYPE "sched"
16 #include "llvm/CodeGen/MachineFunction.h"
17 #include "llvm/CodeGen/SelectionDAGISel.h"
18 #include "llvm/CodeGen/SelectionDAG.h"
19 #include "llvm/CodeGen/SSARegMap.h"
20 #include "llvm/Target/TargetMachine.h"
21 #include "llvm/Target/TargetInstrInfo.h"
22 #include "llvm/Support/CommandLine.h"
27 ViewDAGs("view-sched-dags", cl::Hidden,
28 cl::desc("Pop up a window to show sched dags as they are processed"));
30 static const bool ViewDAGS = 0;
36 MachineBasicBlock *BB;
37 const TargetMachine &TM;
38 const TargetInstrInfo &TII;
39 const MRegisterInfo &MRI;
42 std::map<SDNode *, unsigned> EmittedOps;
44 SimpleSched(SelectionDAG &D, MachineBasicBlock *bb)
45 : DAG(D), BB(bb), TM(D.getTarget()), TII(*TM.getInstrInfo()),
46 MRI(*TM.getRegisterInfo()), RegMap(BB->getParent()->getSSARegMap()) {
47 assert(&TII && "Target doesn't provide instr info?");
48 assert(&MRI && "Target doesn't provide register info?");
56 unsigned Emit(SDOperand Op);
60 unsigned SimpleSched::Emit(SDOperand Op) {
61 // Check to see if we have already emitted this. If so, return the value
62 // already emitted. Note that if a node has a single use it cannot be
63 // revisited, so don't bother putting it in the map.
65 if (Op.Val->hasOneUse()) {
66 OpSlot = 0; // No reuse possible.
68 std::map<SDNode *, unsigned>::iterator OpI = EmittedOps.lower_bound(Op.Val);
69 if (OpI != EmittedOps.end() && OpI->first == Op.Val)
70 return OpI->second + Op.ResNo;
71 OpSlot = &EmittedOps.insert(OpI, std::make_pair(Op.Val, 0))->second;
74 unsigned ResultReg = 0;
75 if (Op.isTargetOpcode()) {
76 unsigned Opc = Op.getTargetOpcode();
77 const TargetInstrDescriptor &II = TII.get(Opc);
79 // The results of target nodes have register or immediate operands first,
80 // then an optional chain, and optional flag operands (which do not go into
81 // the machine instrs).
82 unsigned NumResults = Op.Val->getNumValues();
83 while (NumResults && Op.Val->getValueType(NumResults-1) == MVT::Flag)
85 if (NumResults && Op.Val->getValueType(NumResults-1) == MVT::Other)
86 --NumResults; // Skip over chain result.
88 // The inputs to target nodes have any actual inputs first, followed by an
89 // optional chain operand, then flag operands. Compute the number of actual
90 // operands that will go into the machine instr.
91 unsigned NodeOperands = Op.getNumOperands();
92 while (NodeOperands &&
93 Op.getOperand(NodeOperands-1).getValueType() == MVT::Flag)
95 if (NodeOperands && // Ignore chain if it exists.
96 Op.getOperand(NodeOperands-1).getValueType() == MVT::Other)
99 unsigned NumMIOperands = NodeOperands+NumResults;
101 assert((unsigned(II.numOperands) == NumMIOperands || II.numOperands == -1)&&
102 "#operands for dag node doesn't match .td file!");
105 // Create the new machine instruction.
106 MachineInstr *MI = new MachineInstr(Opc, NumMIOperands, true, true);
108 // Add result register values for things that are defined by this
111 // Create the result registers for this node and add the result regs to
112 // the machine instruction.
113 const TargetOperandInfo *OpInfo = II.OpInfo;
114 ResultReg = RegMap->createVirtualRegister(OpInfo[0].RegClass);
115 MI->addRegOperand(ResultReg, MachineOperand::Def);
116 for (unsigned i = 1; i != NumResults; ++i) {
117 assert(OpInfo[i].RegClass && "Isn't a register operand!");
118 MI->addRegOperand(RegMap->createVirtualRegister(OpInfo[0].RegClass),
119 MachineOperand::Def);
123 // Emit all of the operands of this instruction, adding them to the
124 // instruction as appropriate.
125 for (unsigned i = 0, e = Op.getNumOperands(); i != e; ++i) {
126 if (Op.getOperand(i).isTargetOpcode()) {
127 // Note that this case is redundant with the final else block, but we
128 // include it because it is the most common and it makes the logic
130 unsigned R = Emit(Op.getOperand(i));
131 // Add an operand, unless this corresponds to a chain or flag node.
132 MVT::ValueType VT = Op.getOperand(i).getValueType();
133 if (VT != MVT::Other && VT != MVT::Flag)
134 MI->addRegOperand(R, MachineOperand::Use);
135 } else if (ConstantSDNode *C =
136 dyn_cast<ConstantSDNode>(Op.getOperand(i))) {
137 MI->addZeroExtImm64Operand(C->getValue());
138 } else if (RegisterSDNode*R =dyn_cast<RegisterSDNode>(Op.getOperand(i))) {
139 MI->addRegOperand(R->getReg(), MachineOperand::Use);
140 } else if (GlobalAddressSDNode *TGA =
141 dyn_cast<GlobalAddressSDNode>(Op.getOperand(i))) {
142 MI->addGlobalAddressOperand(TGA->getGlobal(), false, 0);
143 } else if (BasicBlockSDNode *BB =
144 dyn_cast<BasicBlockSDNode>(Op.getOperand(i))) {
145 MI->addMachineBasicBlockOperand(BB->getBasicBlock());
146 } else if (FrameIndexSDNode *FI =
147 dyn_cast<FrameIndexSDNode>(Op.getOperand(i))) {
148 MI->addFrameIndexOperand(FI->getIndex());
149 } else if (ConstantPoolSDNode *CP =
150 dyn_cast<ConstantPoolSDNode>(Op.getOperand(i))) {
151 MI->addConstantPoolIndexOperand(CP->getIndex());
152 } else if (ExternalSymbolSDNode *ES =
153 dyn_cast<ExternalSymbolSDNode>(Op.getOperand(i))) {
154 MI->addExternalSymbolOperand(ES->getSymbol(), false);
156 unsigned R = Emit(Op.getOperand(i));
157 // Add an operand, unless this corresponds to a chain or flag node.
158 MVT::ValueType VT = Op.getOperand(i).getValueType();
159 if (VT != MVT::Other && VT != MVT::Flag)
160 MI->addRegOperand(R, MachineOperand::Use);
164 // Now that we have emitted all operands, emit this instruction itself.
165 BB->insert(BB->end(), MI);
167 switch (Op.getOpcode()) {
170 assert(0 && "This target-independent node should have been selected!");
171 case ISD::EntryToken: break;
172 case ISD::TokenFactor:
173 for (unsigned i = 0, e = Op.getNumOperands(); i != e; ++i)
174 Emit(Op.getOperand(i));
176 case ISD::CopyToReg: {
177 Emit(Op.getOperand(0)); // Emit the chain.
178 unsigned Val = Emit(Op.getOperand(2));
179 MRI.copyRegToReg(*BB, BB->end(),
180 cast<RegisterSDNode>(Op.getOperand(1))->getReg(), Val,
181 RegMap->getRegClass(Val));
184 case ISD::CopyFromReg: {
185 Emit(Op.getOperand(0)); // Emit the chain.
186 unsigned SrcReg = cast<RegisterSDNode>(Op.getOperand(1))->getReg();
188 // Figure out the register class to create for the destreg.
189 const TargetRegisterClass *TRC = 0;
190 if (MRegisterInfo::isVirtualRegister(SrcReg)) {
191 TRC = RegMap->getRegClass(SrcReg);
193 // FIXME: we don't know what register class to generate this for. Do
194 // a brute force search and pick the first match. :(
195 for (MRegisterInfo::regclass_iterator I = MRI.regclass_begin(),
196 E = MRI.regclass_end(); I != E; ++I)
197 if ((*I)->contains(SrcReg)) {
201 assert(TRC && "Couldn't find register class for reg copy!");
204 // Create the reg, emit the copy.
205 ResultReg = RegMap->createVirtualRegister(TRC);
206 MRI.copyRegToReg(*BB, BB->end(), ResultReg, SrcReg, TRC);
212 if (OpSlot) *OpSlot = ResultReg;
213 return ResultReg+Op.ResNo;
217 /// Pick a safe ordering and emit instructions for each target node in the
219 void SelectionDAGISel::ScheduleAndEmitDAG(SelectionDAG &SD) {
220 if (ViewDAGs) SD.viewGraph();
221 SimpleSched(SD, BB).Run();