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?");
60 unsigned Emit(SDOperand Op);
64 unsigned SimpleSched::Emit(SDOperand Op) {
65 // Check to see if we have already emitted this. If so, return the value
66 // already emitted. Note that if a node has a single use it cannot be
67 // revisited, so don't bother putting it in the map.
69 if (Op.Val->hasOneUse()) {
70 OpSlot = 0; // No reuse possible.
72 std::map<SDNode *, unsigned>::iterator OpI = EmittedOps.lower_bound(Op.Val);
73 if (OpI != EmittedOps.end() && OpI->first == Op.Val)
74 return OpI->second + Op.ResNo;
75 OpSlot = &EmittedOps.insert(OpI, std::make_pair(Op.Val, 0))->second;
78 unsigned ResultReg = 0;
79 if (Op.isTargetOpcode()) {
80 unsigned Opc = Op.getTargetOpcode();
81 const TargetInstrDescriptor &II = TII.get(Opc);
83 // The results of target nodes have register or immediate operands first,
84 // then an optional chain, and optional flag operands (which do not go into
85 // the machine instrs).
86 unsigned NumResults = Op.Val->getNumValues();
87 while (NumResults && Op.Val->getValueType(NumResults-1) == MVT::Flag)
89 if (NumResults && Op.Val->getValueType(NumResults-1) == MVT::Other)
90 --NumResults; // Skip over chain result.
92 // The inputs to target nodes have any actual inputs first, followed by an
93 // optional chain operand, then flag operands. Compute the number of actual
94 // operands that will go into the machine instr.
95 unsigned NodeOperands = Op.getNumOperands();
96 while (NodeOperands &&
97 Op.getOperand(NodeOperands-1).getValueType() == MVT::Flag)
99 if (NodeOperands && // Ignore chain if it exists.
100 Op.getOperand(NodeOperands-1).getValueType() == MVT::Other)
103 unsigned NumMIOperands = NodeOperands+NumResults;
105 assert((unsigned(II.numOperands) == NumMIOperands || II.numOperands == -1)&&
106 "#operands for dag node doesn't match .td file!");
109 // Create the new machine instruction.
110 MachineInstr *MI = new MachineInstr(Opc, NumMIOperands, true, true);
112 // Add result register values for things that are defined by this
115 // Create the result registers for this node and add the result regs to
116 // the machine instruction.
117 const TargetOperandInfo *OpInfo = II.OpInfo;
118 ResultReg = RegMap->createVirtualRegister(OpInfo[0].RegClass);
119 MI->addRegOperand(ResultReg, MachineOperand::Def);
120 for (unsigned i = 1; i != NumResults; ++i) {
121 assert(OpInfo[i].RegClass && "Isn't a register operand!");
122 MI->addRegOperand(RegMap->createVirtualRegister(OpInfo[0].RegClass),
123 MachineOperand::Def);
127 // Emit all of the operands of this instruction, adding them to the
128 // instruction as appropriate.
129 for (unsigned i = 0, e = Op.getNumOperands(); i != e; ++i) {
130 if (Op.getOperand(i).isTargetOpcode()) {
131 // Note that this case is redundant with the final else block, but we
132 // include it because it is the most common and it makes the logic
134 unsigned R = Emit(Op.getOperand(i));
135 // Add an operand, unless this corresponds to a chain or flag node.
136 MVT::ValueType VT = Op.getOperand(i).getValueType();
137 if (VT != MVT::Other && VT != MVT::Flag)
138 MI->addRegOperand(R, MachineOperand::Use);
139 } else if (ConstantSDNode *C =
140 dyn_cast<ConstantSDNode>(Op.getOperand(i))) {
141 MI->addZeroExtImm64Operand(C->getValue());
142 } else if (RegisterSDNode*R =dyn_cast<RegisterSDNode>(Op.getOperand(i))) {
143 MI->addRegOperand(R->getReg(), MachineOperand::Use);
144 } else if (GlobalAddressSDNode *TGA =
145 dyn_cast<GlobalAddressSDNode>(Op.getOperand(i))) {
146 MI->addGlobalAddressOperand(TGA->getGlobal(), false, 0);
147 } else if (BasicBlockSDNode *BB =
148 dyn_cast<BasicBlockSDNode>(Op.getOperand(i))) {
149 MI->addMachineBasicBlockOperand(BB->getBasicBlock());
150 } else if (FrameIndexSDNode *FI =
151 dyn_cast<FrameIndexSDNode>(Op.getOperand(i))) {
152 MI->addFrameIndexOperand(FI->getIndex());
153 } else if (ConstantPoolSDNode *CP =
154 dyn_cast<ConstantPoolSDNode>(Op.getOperand(i))) {
155 unsigned Idx = ConstPool->getConstantPoolIndex(CP->get());
156 MI->addConstantPoolIndexOperand(Idx);
157 } else if (ExternalSymbolSDNode *ES =
158 dyn_cast<ExternalSymbolSDNode>(Op.getOperand(i))) {
159 MI->addExternalSymbolOperand(ES->getSymbol(), false);
161 unsigned R = Emit(Op.getOperand(i));
162 // Add an operand, unless this corresponds to a chain or flag node.
163 MVT::ValueType VT = Op.getOperand(i).getValueType();
164 if (VT != MVT::Other && VT != MVT::Flag)
165 MI->addRegOperand(R, MachineOperand::Use);
169 // Now that we have emitted all operands, emit this instruction itself.
170 if ((II.Flags & M_USES_CUSTOM_DAG_SCHED_INSERTION) == 0) {
171 BB->insert(BB->end(), MI);
173 // Insert this instruction into the end of the basic block, potentially
174 // taking some custom action.
175 BB = DAG.getTargetLoweringInfo().InsertAtEndOfBasicBlock(MI, BB);
178 switch (Op.getOpcode()) {
181 assert(0 && "This target-independent node should have been selected!");
182 case ISD::EntryToken: break;
183 case ISD::TokenFactor:
184 for (unsigned i = 0, e = Op.getNumOperands(); i != e; ++i)
185 Emit(Op.getOperand(i));
187 case ISD::CopyToReg: {
188 Emit(Op.getOperand(0)); // Emit the chain.
189 unsigned Val = Emit(Op.getOperand(2));
190 MRI.copyRegToReg(*BB, BB->end(),
191 cast<RegisterSDNode>(Op.getOperand(1))->getReg(), Val,
192 RegMap->getRegClass(Val));
195 case ISD::CopyFromReg: {
196 Emit(Op.getOperand(0)); // Emit the chain.
197 unsigned SrcReg = cast<RegisterSDNode>(Op.getOperand(1))->getReg();
199 // Figure out the register class to create for the destreg.
200 const TargetRegisterClass *TRC = 0;
201 if (MRegisterInfo::isVirtualRegister(SrcReg)) {
202 TRC = RegMap->getRegClass(SrcReg);
204 // FIXME: we don't know what register class to generate this for. Do
205 // a brute force search and pick the first match. :(
206 for (MRegisterInfo::regclass_iterator I = MRI.regclass_begin(),
207 E = MRI.regclass_end(); I != E; ++I)
208 if ((*I)->contains(SrcReg)) {
212 assert(TRC && "Couldn't find register class for reg copy!");
215 // Create the reg, emit the copy.
216 ResultReg = RegMap->createVirtualRegister(TRC);
217 MRI.copyRegToReg(*BB, BB->end(), ResultReg, SrcReg, TRC);
223 if (OpSlot) *OpSlot = ResultReg;
224 return ResultReg+Op.ResNo;
228 /// Pick a safe ordering and emit instructions for each target node in the
230 void SelectionDAGISel::ScheduleAndEmitDAG(SelectionDAG &SD) {
231 if (ViewDAGs) SD.viewGraph();
232 SimpleSched(SD, BB).Run();