blob: dc3996bbfe156773a1d405adbece5bd26d61ab7f [file] [log] [blame]
Chris Lattnerd32b2362005-08-18 18:45:24 +00001//===-- ScheduleDAG.cpp - Implement a trivial DAG scheduler ---------------===//
2//
3// The LLVM Compiler Infrastructure
4//
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.
7//
8//===----------------------------------------------------------------------===//
9//
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.
12//
13//===----------------------------------------------------------------------===//
14
15#define DEBUG_TYPE "sched"
Chris Lattner5839bf22005-08-26 17:15:30 +000016#include "llvm/CodeGen/MachineConstantPool.h"
Chris Lattner4ccd4062005-08-19 20:45:43 +000017#include "llvm/CodeGen/MachineFunction.h"
Chris Lattnerd32b2362005-08-18 18:45:24 +000018#include "llvm/CodeGen/SelectionDAGISel.h"
Chris Lattner2d973e42005-08-18 20:07:59 +000019#include "llvm/CodeGen/SelectionDAG.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"
Chris Lattner025c39b2005-08-26 20:54:47 +000023#include "llvm/Target/TargetLowering.h"
Chris Lattner068ca152005-08-18 20:11:49 +000024#include "llvm/Support/CommandLine.h"
Chris Lattnerd32b2362005-08-18 18:45:24 +000025using namespace llvm;
26
Chris Lattner068ca152005-08-18 20:11:49 +000027#ifndef _NDEBUG
28static cl::opt<bool>
29ViewDAGs("view-sched-dags", cl::Hidden,
30 cl::desc("Pop up a window to show sched dags as they are processed"));
31#else
32static const bool ViewDAGS = 0;
33#endif
34
Chris Lattner2d973e42005-08-18 20:07:59 +000035namespace {
36 class SimpleSched {
37 SelectionDAG &DAG;
38 MachineBasicBlock *BB;
39 const TargetMachine &TM;
40 const TargetInstrInfo &TII;
Chris Lattner01891972005-08-19 20:50:53 +000041 const MRegisterInfo &MRI;
Chris Lattner4ccd4062005-08-19 20:45:43 +000042 SSARegMap *RegMap;
Chris Lattner5839bf22005-08-26 17:15:30 +000043 MachineConstantPool *ConstPool;
Chris Lattner2d973e42005-08-18 20:07:59 +000044
45 std::map<SDNode *, unsigned> EmittedOps;
46 public:
47 SimpleSched(SelectionDAG &D, MachineBasicBlock *bb)
Chris Lattner4ccd4062005-08-19 20:45:43 +000048 : DAG(D), BB(bb), TM(D.getTarget()), TII(*TM.getInstrInfo()),
Chris Lattner5839bf22005-08-26 17:15:30 +000049 MRI(*TM.getRegisterInfo()), RegMap(BB->getParent()->getSSARegMap()),
50 ConstPool(BB->getParent()->getConstantPool()) {
Chris Lattner2d973e42005-08-18 20:07:59 +000051 assert(&TII && "Target doesn't provide instr info?");
Chris Lattner01891972005-08-19 20:50:53 +000052 assert(&MRI && "Target doesn't provide register info?");
Chris Lattner2d973e42005-08-18 20:07:59 +000053 }
54
55 void Run() {
56 Emit(DAG.getRoot());
57 }
58
59 private:
60 unsigned Emit(SDOperand Op);
61 };
62}
63
64unsigned 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.
68 unsigned *OpSlot;
69 if (Op.Val->hasOneUse()) {
70 OpSlot = 0; // No reuse possible.
71 } else {
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;
76 }
77
78 unsigned ResultReg = 0;
79 if (Op.isTargetOpcode()) {
80 unsigned Opc = Op.getTargetOpcode();
81 const TargetInstrDescriptor &II = TII.get(Opc);
82
Chris Lattner376d54f2005-08-25 17:48:54 +000083 // 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).
Chris Lattner4ccd4062005-08-19 20:45:43 +000086 unsigned NumResults = Op.Val->getNumValues();
Chris Lattner376d54f2005-08-25 17:48:54 +000087 while (NumResults && Op.Val->getValueType(NumResults-1) == MVT::Flag)
Chris Lattner4ccd4062005-08-19 20:45:43 +000088 --NumResults;
Chris Lattner376d54f2005-08-25 17:48:54 +000089 if (NumResults && Op.Val->getValueType(NumResults-1) == MVT::Other)
90 --NumResults; // Skip over chain result.
Chris Lattner14b392a2005-08-24 22:02:41 +000091
Chris Lattner376d54f2005-08-25 17:48:54 +000092 // 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.
Chris Lattner14b392a2005-08-24 22:02:41 +000095 unsigned NodeOperands = Op.getNumOperands();
Chris Lattner376d54f2005-08-25 17:48:54 +000096 while (NodeOperands &&
97 Op.getOperand(NodeOperands-1).getValueType() == MVT::Flag)
98 --NodeOperands;
Chris Lattner14b392a2005-08-24 22:02:41 +000099 if (NodeOperands && // Ignore chain if it exists.
100 Op.getOperand(NodeOperands-1).getValueType() == MVT::Other)
101 --NodeOperands;
102
103 unsigned NumMIOperands = NodeOperands+NumResults;
Chris Lattnerca6aa2f2005-08-19 01:01:34 +0000104#ifndef _NDEBUG
Chris Lattner14b392a2005-08-24 22:02:41 +0000105 assert((unsigned(II.numOperands) == NumMIOperands || II.numOperands == -1)&&
Chris Lattner2d973e42005-08-18 20:07:59 +0000106 "#operands for dag node doesn't match .td file!");
Chris Lattnerca6aa2f2005-08-19 01:01:34 +0000107#endif
Chris Lattner2d973e42005-08-18 20:07:59 +0000108
109 // Create the new machine instruction.
Chris Lattner14b392a2005-08-24 22:02:41 +0000110 MachineInstr *MI = new MachineInstr(Opc, NumMIOperands, true, true);
Chris Lattner2d973e42005-08-18 20:07:59 +0000111
112 // Add result register values for things that are defined by this
113 // instruction.
Chris Lattner4ccd4062005-08-19 20:45:43 +0000114 if (NumResults) {
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);
124 }
125 }
Chris Lattner2d973e42005-08-18 20:07:59 +0000126
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) {
Chris Lattner23553cf2005-08-22 01:04:32 +0000130 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
133 // simpler here.
134 unsigned R = Emit(Op.getOperand(i));
Chris Lattner376d54f2005-08-25 17:48:54 +0000135 // 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)
Chris Lattner23553cf2005-08-22 01:04:32 +0000138 MI->addRegOperand(R, MachineOperand::Use);
139 } else if (ConstantSDNode *C =
140 dyn_cast<ConstantSDNode>(Op.getOperand(i))) {
Chris Lattner2d973e42005-08-18 20:07:59 +0000141 MI->addZeroExtImm64Operand(C->getValue());
142 } else if (RegisterSDNode*R =dyn_cast<RegisterSDNode>(Op.getOperand(i))) {
143 MI->addRegOperand(R->getReg(), MachineOperand::Use);
Chris Lattner9b78db72005-08-19 22:38:24 +0000144 } else if (GlobalAddressSDNode *TGA =
145 dyn_cast<GlobalAddressSDNode>(Op.getOperand(i))) {
146 MI->addGlobalAddressOperand(TGA->getGlobal(), false, 0);
Chris Lattnerf85ab152005-08-21 18:49:29 +0000147 } else if (BasicBlockSDNode *BB =
148 dyn_cast<BasicBlockSDNode>(Op.getOperand(i))) {
149 MI->addMachineBasicBlockOperand(BB->getBasicBlock());
Chris Lattner81e72b12005-08-21 19:56:04 +0000150 } else if (FrameIndexSDNode *FI =
151 dyn_cast<FrameIndexSDNode>(Op.getOperand(i))) {
152 MI->addFrameIndexOperand(FI->getIndex());
Chris Lattner23553cf2005-08-22 01:04:32 +0000153 } else if (ConstantPoolSDNode *CP =
154 dyn_cast<ConstantPoolSDNode>(Op.getOperand(i))) {
Chris Lattner5839bf22005-08-26 17:15:30 +0000155 unsigned Idx = ConstPool->getConstantPoolIndex(CP->get());
156 MI->addConstantPoolIndexOperand(Idx);
Chris Lattner14b392a2005-08-24 22:02:41 +0000157 } else if (ExternalSymbolSDNode *ES =
158 dyn_cast<ExternalSymbolSDNode>(Op.getOperand(i))) {
159 MI->addExternalSymbolOperand(ES->getSymbol(), false);
Chris Lattner2d973e42005-08-18 20:07:59 +0000160 } else {
161 unsigned R = Emit(Op.getOperand(i));
Chris Lattner376d54f2005-08-25 17:48:54 +0000162 // 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)
Chris Lattner2d973e42005-08-18 20:07:59 +0000165 MI->addRegOperand(R, MachineOperand::Use);
166 }
167 }
168
169 // Now that we have emitted all operands, emit this instruction itself.
Chris Lattner025c39b2005-08-26 20:54:47 +0000170 if ((II.Flags & M_USES_CUSTOM_DAG_SCHED_INSERTION) == 0) {
171 BB->insert(BB->end(), MI);
172 } else {
173 // Insert this instruction into the end of the basic block, potentially
174 // taking some custom action.
175 BB = DAG.getTargetLoweringInfo().InsertAtEndOfBasicBlock(MI, BB);
176 }
Chris Lattner2d973e42005-08-18 20:07:59 +0000177 } else {
178 switch (Op.getOpcode()) {
Chris Lattnerca6aa2f2005-08-19 01:01:34 +0000179 default:
180 Op.Val->dump();
181 assert(0 && "This target-independent node should have been selected!");
Chris Lattner81e72b12005-08-21 19:56:04 +0000182 case ISD::EntryToken: break;
Chris Lattner7ef33042005-08-19 21:43:53 +0000183 case ISD::TokenFactor:
184 for (unsigned i = 0, e = Op.getNumOperands(); i != e; ++i)
185 Emit(Op.getOperand(i));
186 break;
Chris Lattnerca6aa2f2005-08-19 01:01:34 +0000187 case ISD::CopyToReg: {
Chris Lattner7ef33042005-08-19 21:43:53 +0000188 Emit(Op.getOperand(0)); // Emit the chain.
Chris Lattnerca6aa2f2005-08-19 01:01:34 +0000189 unsigned Val = Emit(Op.getOperand(2));
Chris Lattner01891972005-08-19 20:50:53 +0000190 MRI.copyRegToReg(*BB, BB->end(),
191 cast<RegisterSDNode>(Op.getOperand(1))->getReg(), Val,
192 RegMap->getRegClass(Val));
Chris Lattnerca6aa2f2005-08-19 01:01:34 +0000193 break;
194 }
Chris Lattner7ef33042005-08-19 21:43:53 +0000195 case ISD::CopyFromReg: {
196 Emit(Op.getOperand(0)); // Emit the chain.
197 unsigned SrcReg = cast<RegisterSDNode>(Op.getOperand(1))->getReg();
198
199 // Figure out the register class to create for the destreg.
Chris Lattnerfe0c2c82005-08-20 18:07:27 +0000200 const TargetRegisterClass *TRC = 0;
Chris Lattner7ef33042005-08-19 21:43:53 +0000201 if (MRegisterInfo::isVirtualRegister(SrcReg)) {
202 TRC = RegMap->getRegClass(SrcReg);
203 } else {
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)) {
209 TRC = *I;
210 break;
211 }
212 assert(TRC && "Couldn't find register class for reg copy!");
213 }
214
215 // Create the reg, emit the copy.
216 ResultReg = RegMap->createVirtualRegister(TRC);
217 MRI.copyRegToReg(*BB, BB->end(), ResultReg, SrcReg, TRC);
218 break;
219 }
Chris Lattner2d973e42005-08-18 20:07:59 +0000220 }
221 }
222
223 if (OpSlot) *OpSlot = ResultReg;
224 return ResultReg+Op.ResNo;
225}
226
227
Chris Lattnerd32b2362005-08-18 18:45:24 +0000228/// Pick a safe ordering and emit instructions for each target node in the
229/// graph.
230void SelectionDAGISel::ScheduleAndEmitDAG(SelectionDAG &SD) {
Chris Lattner068ca152005-08-18 20:11:49 +0000231 if (ViewDAGs) SD.viewGraph();
Chris Lattner2d973e42005-08-18 20:07:59 +0000232 SimpleSched(SD, BB).Run();
Chris Lattnerd32b2362005-08-18 18:45:24 +0000233}