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/MachineConstantPool.h"
17 #include "llvm/CodeGen/MachineFunction.h"
18 #include "llvm/CodeGen/SelectionDAGISel.h"
19 #include "llvm/CodeGen/SelectionDAG.h"
20 #include "llvm/CodeGen/SSARegMap.h"
21 #include "llvm/Target/TargetMachine.h"
22 #include "llvm/Target/TargetInstrInfo.h"
23 #include "llvm/Target/TargetLowering.h"
24 #include "llvm/Support/CommandLine.h"
29 ViewDAGs("view-sched-dags", cl::Hidden,
30 cl::desc("Pop up a window to show sched dags as they are processed"));
32 static const bool ViewDAGS = 0;
38 MachineBasicBlock *BB;
39 const TargetMachine &TM;
40 const TargetInstrInfo &TII;
41 const MRegisterInfo &MRI;
43 MachineConstantPool *ConstPool;
45 std::map<SDNode *, unsigned> EmittedOps;
47 SimpleSched(SelectionDAG &D, MachineBasicBlock *bb)
48 : DAG(D), BB(bb), TM(D.getTarget()), TII(*TM.getInstrInfo()),
49 MRI(*TM.getRegisterInfo()), RegMap(BB->getParent()->getSSARegMap()),
50 ConstPool(BB->getParent()->getConstantPool()) {
51 assert(&TII && "Target doesn't provide instr info?");
52 assert(&MRI && "Target doesn't provide register info?");
55 MachineBasicBlock *Run() {
61 unsigned Emit(SDOperand Op);
65 unsigned SimpleSched::Emit(SDOperand Op) {
66 // Check to see if we have already emitted this. If so, return the value
67 // already emitted. Note that if a node has a single use it cannot be
68 // revisited, so don't bother putting it in the map.
70 if (Op.Val->hasOneUse()) {
71 OpSlot = 0; // No reuse possible.
73 std::map<SDNode *, unsigned>::iterator OpI = EmittedOps.lower_bound(Op.Val);
74 if (OpI != EmittedOps.end() && OpI->first == Op.Val)
75 return OpI->second + Op.ResNo;
76 OpSlot = &EmittedOps.insert(OpI, std::make_pair(Op.Val, 0))->second;
79 unsigned ResultReg = 0;
80 if (Op.isTargetOpcode()) {
81 unsigned Opc = Op.getTargetOpcode();
82 const TargetInstrDescriptor &II = TII.get(Opc);
84 // The results of target nodes have register or immediate operands first,
85 // then an optional chain, and optional flag operands (which do not go into
86 // the machine instrs).
87 unsigned NumResults = Op.Val->getNumValues();
88 while (NumResults && Op.Val->getValueType(NumResults-1) == MVT::Flag)
90 if (NumResults && Op.Val->getValueType(NumResults-1) == MVT::Other)
91 --NumResults; // Skip over chain result.
93 // The inputs to target nodes have any actual inputs first, followed by an
94 // optional chain operand, then flag operands. Compute the number of actual
95 // operands that will go into the machine instr.
96 unsigned NodeOperands = Op.getNumOperands();
97 while (NodeOperands &&
98 Op.getOperand(NodeOperands-1).getValueType() == MVT::Flag)
100 if (NodeOperands && // Ignore chain if it exists.
101 Op.getOperand(NodeOperands-1).getValueType() == MVT::Other)
104 unsigned NumMIOperands = NodeOperands+NumResults;
106 assert((unsigned(II.numOperands) == NumMIOperands || II.numOperands == -1)&&
107 "#operands for dag node doesn't match .td file!");
110 // Create the new machine instruction.
111 MachineInstr *MI = new MachineInstr(Opc, NumMIOperands, true, true);
113 // Add result register values for things that are defined by this
116 // Create the result registers for this node and add the result regs to
117 // the machine instruction.
118 const TargetOperandInfo *OpInfo = II.OpInfo;
119 ResultReg = RegMap->createVirtualRegister(OpInfo[0].RegClass);
120 MI->addRegOperand(ResultReg, MachineOperand::Def);
121 for (unsigned i = 1; i != NumResults; ++i) {
122 assert(OpInfo[i].RegClass && "Isn't a register operand!");
123 MI->addRegOperand(RegMap->createVirtualRegister(OpInfo[0].RegClass),
124 MachineOperand::Def);
128 // Emit all of the operands of this instruction, adding them to the
129 // instruction as appropriate.
130 for (unsigned i = 0, e = Op.getNumOperands(); i != e; ++i) {
131 if (Op.getOperand(i).isTargetOpcode()) {
132 // Note that this case is redundant with the final else block, but we
133 // include it because it is the most common and it makes the logic
135 unsigned R = Emit(Op.getOperand(i));
136 // Add an operand, unless this corresponds to a chain or flag node.
137 MVT::ValueType VT = Op.getOperand(i).getValueType();
138 if (VT != MVT::Other && VT != MVT::Flag)
139 MI->addRegOperand(R, MachineOperand::Use);
140 } else if (ConstantSDNode *C =
141 dyn_cast<ConstantSDNode>(Op.getOperand(i))) {
142 MI->addZeroExtImm64Operand(C->getValue());
143 } else if (RegisterSDNode*R =dyn_cast<RegisterSDNode>(Op.getOperand(i))) {
144 MI->addRegOperand(R->getReg(), MachineOperand::Use);
145 } else if (GlobalAddressSDNode *TGA =
146 dyn_cast<GlobalAddressSDNode>(Op.getOperand(i))) {
147 MI->addGlobalAddressOperand(TGA->getGlobal(), false, 0);
148 } else if (BasicBlockSDNode *BB =
149 dyn_cast<BasicBlockSDNode>(Op.getOperand(i))) {
150 MI->addMachineBasicBlockOperand(BB->getBasicBlock());
151 } else if (FrameIndexSDNode *FI =
152 dyn_cast<FrameIndexSDNode>(Op.getOperand(i))) {
153 MI->addFrameIndexOperand(FI->getIndex());
154 } else if (ConstantPoolSDNode *CP =
155 dyn_cast<ConstantPoolSDNode>(Op.getOperand(i))) {
156 unsigned Idx = ConstPool->getConstantPoolIndex(CP->get());
157 MI->addConstantPoolIndexOperand(Idx);
158 } else if (ExternalSymbolSDNode *ES =
159 dyn_cast<ExternalSymbolSDNode>(Op.getOperand(i))) {
160 MI->addExternalSymbolOperand(ES->getSymbol(), false);
162 unsigned R = Emit(Op.getOperand(i));
163 // Add an operand, unless this corresponds to a chain or flag node.
164 MVT::ValueType VT = Op.getOperand(i).getValueType();
165 if (VT != MVT::Other && VT != MVT::Flag)
166 MI->addRegOperand(R, MachineOperand::Use);
170 // Now that we have emitted all operands, emit this instruction itself.
171 if ((II.Flags & M_USES_CUSTOM_DAG_SCHED_INSERTION) == 0) {
172 BB->insert(BB->end(), MI);
174 // Insert this instruction into the end of the basic block, potentially
175 // taking some custom action.
176 BB = DAG.getTargetLoweringInfo().InsertAtEndOfBasicBlock(MI, BB);
179 switch (Op.getOpcode()) {
182 assert(0 && "This target-independent node should have been selected!");
183 case ISD::EntryToken: break;
184 case ISD::TokenFactor:
185 for (unsigned i = 0, e = Op.getNumOperands(); i != e; ++i)
186 Emit(Op.getOperand(i));
188 case ISD::CopyToReg: {
189 Emit(Op.getOperand(0)); // Emit the chain.
190 unsigned Val = Emit(Op.getOperand(2));
191 MRI.copyRegToReg(*BB, BB->end(),
192 cast<RegisterSDNode>(Op.getOperand(1))->getReg(), Val,
193 RegMap->getRegClass(Val));
196 case ISD::CopyFromReg: {
197 Emit(Op.getOperand(0)); // Emit the chain.
198 unsigned SrcReg = cast<RegisterSDNode>(Op.getOperand(1))->getReg();
200 // Figure out the register class to create for the destreg.
201 const TargetRegisterClass *TRC = 0;
202 if (MRegisterInfo::isVirtualRegister(SrcReg)) {
203 TRC = RegMap->getRegClass(SrcReg);
205 // FIXME: we don't know what register class to generate this for. Do
206 // a brute force search and pick the first match. :(
207 for (MRegisterInfo::regclass_iterator I = MRI.regclass_begin(),
208 E = MRI.regclass_end(); I != E; ++I)
209 if ((*I)->contains(SrcReg)) {
213 assert(TRC && "Couldn't find register class for reg copy!");
216 // Create the reg, emit the copy.
217 ResultReg = RegMap->createVirtualRegister(TRC);
218 MRI.copyRegToReg(*BB, BB->end(), ResultReg, SrcReg, TRC);
224 if (OpSlot) *OpSlot = ResultReg;
225 return ResultReg+Op.ResNo;
229 /// Pick a safe ordering and emit instructions for each target node in the
231 void SelectionDAGISel::ScheduleAndEmitDAG(SelectionDAG &SD) {
232 if (ViewDAGs) SD.viewGraph();
233 BB = SimpleSched(SD, BB).Run();