blob: 723daed42f01ec547d60c9fce232f5ffbba977dc [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
Chris Lattner620c93c2005-08-27 00:58:02 +000055 MachineBasicBlock *Run() {
Chris Lattner2d973e42005-08-18 20:07:59 +000056 Emit(DAG.getRoot());
Chris Lattner620c93c2005-08-27 00:58:02 +000057 return BB;
Chris Lattner2d973e42005-08-18 20:07:59 +000058 }
59
60 private:
61 unsigned Emit(SDOperand Op);
62 };
63}
64
65unsigned 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.
69 unsigned *OpSlot;
70 if (Op.Val->hasOneUse()) {
71 OpSlot = 0; // No reuse possible.
72 } else {
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;
77 }
78
79 unsigned ResultReg = 0;
80 if (Op.isTargetOpcode()) {
81 unsigned Opc = Op.getTargetOpcode();
82 const TargetInstrDescriptor &II = TII.get(Opc);
83
Chris Lattner376d54f2005-08-25 17:48:54 +000084 // 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).
Chris Lattner4ccd4062005-08-19 20:45:43 +000087 unsigned NumResults = Op.Val->getNumValues();
Chris Lattner376d54f2005-08-25 17:48:54 +000088 while (NumResults && Op.Val->getValueType(NumResults-1) == MVT::Flag)
Chris Lattner4ccd4062005-08-19 20:45:43 +000089 --NumResults;
Chris Lattner376d54f2005-08-25 17:48:54 +000090 if (NumResults && Op.Val->getValueType(NumResults-1) == MVT::Other)
91 --NumResults; // Skip over chain result.
Chris Lattner14b392a2005-08-24 22:02:41 +000092
Chris Lattner376d54f2005-08-25 17:48:54 +000093 // 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.
Chris Lattner14b392a2005-08-24 22:02:41 +000096 unsigned NodeOperands = Op.getNumOperands();
Chris Lattner376d54f2005-08-25 17:48:54 +000097 while (NodeOperands &&
98 Op.getOperand(NodeOperands-1).getValueType() == MVT::Flag)
99 --NodeOperands;
Chris Lattner14b392a2005-08-24 22:02:41 +0000100 if (NodeOperands && // Ignore chain if it exists.
101 Op.getOperand(NodeOperands-1).getValueType() == MVT::Other)
102 --NodeOperands;
103
104 unsigned NumMIOperands = NodeOperands+NumResults;
Chris Lattnerca6aa2f2005-08-19 01:01:34 +0000105#ifndef _NDEBUG
Chris Lattner14b392a2005-08-24 22:02:41 +0000106 assert((unsigned(II.numOperands) == NumMIOperands || II.numOperands == -1)&&
Chris Lattner2d973e42005-08-18 20:07:59 +0000107 "#operands for dag node doesn't match .td file!");
Chris Lattnerca6aa2f2005-08-19 01:01:34 +0000108#endif
Chris Lattner2d973e42005-08-18 20:07:59 +0000109
110 // Create the new machine instruction.
Chris Lattner14b392a2005-08-24 22:02:41 +0000111 MachineInstr *MI = new MachineInstr(Opc, NumMIOperands, true, true);
Chris Lattner2d973e42005-08-18 20:07:59 +0000112
113 // Add result register values for things that are defined by this
114 // instruction.
Chris Lattner4ccd4062005-08-19 20:45:43 +0000115 if (NumResults) {
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);
125 }
126 }
Chris Lattner2d973e42005-08-18 20:07:59 +0000127
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) {
Chris Lattner23553cf2005-08-22 01:04:32 +0000131 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
134 // simpler here.
135 unsigned R = Emit(Op.getOperand(i));
Chris Lattner376d54f2005-08-25 17:48:54 +0000136 // 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)
Chris Lattner23553cf2005-08-22 01:04:32 +0000139 MI->addRegOperand(R, MachineOperand::Use);
140 } else if (ConstantSDNode *C =
141 dyn_cast<ConstantSDNode>(Op.getOperand(i))) {
Chris Lattner2d973e42005-08-18 20:07:59 +0000142 MI->addZeroExtImm64Operand(C->getValue());
143 } else if (RegisterSDNode*R =dyn_cast<RegisterSDNode>(Op.getOperand(i))) {
144 MI->addRegOperand(R->getReg(), MachineOperand::Use);
Chris Lattner9b78db72005-08-19 22:38:24 +0000145 } else if (GlobalAddressSDNode *TGA =
146 dyn_cast<GlobalAddressSDNode>(Op.getOperand(i))) {
147 MI->addGlobalAddressOperand(TGA->getGlobal(), false, 0);
Chris Lattnerf85ab152005-08-21 18:49:29 +0000148 } else if (BasicBlockSDNode *BB =
149 dyn_cast<BasicBlockSDNode>(Op.getOperand(i))) {
150 MI->addMachineBasicBlockOperand(BB->getBasicBlock());
Chris Lattner81e72b12005-08-21 19:56:04 +0000151 } else if (FrameIndexSDNode *FI =
152 dyn_cast<FrameIndexSDNode>(Op.getOperand(i))) {
153 MI->addFrameIndexOperand(FI->getIndex());
Chris Lattner23553cf2005-08-22 01:04:32 +0000154 } else if (ConstantPoolSDNode *CP =
155 dyn_cast<ConstantPoolSDNode>(Op.getOperand(i))) {
Chris Lattner5839bf22005-08-26 17:15:30 +0000156 unsigned Idx = ConstPool->getConstantPoolIndex(CP->get());
157 MI->addConstantPoolIndexOperand(Idx);
Chris Lattner14b392a2005-08-24 22:02:41 +0000158 } else if (ExternalSymbolSDNode *ES =
159 dyn_cast<ExternalSymbolSDNode>(Op.getOperand(i))) {
160 MI->addExternalSymbolOperand(ES->getSymbol(), false);
Chris Lattner2d973e42005-08-18 20:07:59 +0000161 } else {
162 unsigned R = Emit(Op.getOperand(i));
Chris Lattner376d54f2005-08-25 17:48:54 +0000163 // 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)
Chris Lattner2d973e42005-08-18 20:07:59 +0000166 MI->addRegOperand(R, MachineOperand::Use);
167 }
168 }
169
170 // Now that we have emitted all operands, emit this instruction itself.
Chris Lattner025c39b2005-08-26 20:54:47 +0000171 if ((II.Flags & M_USES_CUSTOM_DAG_SCHED_INSERTION) == 0) {
172 BB->insert(BB->end(), MI);
173 } else {
174 // Insert this instruction into the end of the basic block, potentially
175 // taking some custom action.
176 BB = DAG.getTargetLoweringInfo().InsertAtEndOfBasicBlock(MI, BB);
177 }
Chris Lattner2d973e42005-08-18 20:07:59 +0000178 } else {
179 switch (Op.getOpcode()) {
Chris Lattnerca6aa2f2005-08-19 01:01:34 +0000180 default:
181 Op.Val->dump();
182 assert(0 && "This target-independent node should have been selected!");
Chris Lattner81e72b12005-08-21 19:56:04 +0000183 case ISD::EntryToken: break;
Chris Lattner7ef33042005-08-19 21:43:53 +0000184 case ISD::TokenFactor:
185 for (unsigned i = 0, e = Op.getNumOperands(); i != e; ++i)
186 Emit(Op.getOperand(i));
187 break;
Chris Lattnerca6aa2f2005-08-19 01:01:34 +0000188 case ISD::CopyToReg: {
Chris Lattner7ef33042005-08-19 21:43:53 +0000189 Emit(Op.getOperand(0)); // Emit the chain.
Chris Lattnerca6aa2f2005-08-19 01:01:34 +0000190 unsigned Val = Emit(Op.getOperand(2));
Chris Lattner01891972005-08-19 20:50:53 +0000191 MRI.copyRegToReg(*BB, BB->end(),
192 cast<RegisterSDNode>(Op.getOperand(1))->getReg(), Val,
193 RegMap->getRegClass(Val));
Chris Lattnerca6aa2f2005-08-19 01:01:34 +0000194 break;
195 }
Chris Lattner7ef33042005-08-19 21:43:53 +0000196 case ISD::CopyFromReg: {
197 Emit(Op.getOperand(0)); // Emit the chain.
198 unsigned SrcReg = cast<RegisterSDNode>(Op.getOperand(1))->getReg();
199
200 // Figure out the register class to create for the destreg.
Chris Lattnerfe0c2c82005-08-20 18:07:27 +0000201 const TargetRegisterClass *TRC = 0;
Chris Lattner7ef33042005-08-19 21:43:53 +0000202 if (MRegisterInfo::isVirtualRegister(SrcReg)) {
203 TRC = RegMap->getRegClass(SrcReg);
204 } else {
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)) {
210 TRC = *I;
211 break;
212 }
213 assert(TRC && "Couldn't find register class for reg copy!");
214 }
215
216 // Create the reg, emit the copy.
217 ResultReg = RegMap->createVirtualRegister(TRC);
218 MRI.copyRegToReg(*BB, BB->end(), ResultReg, SrcReg, TRC);
219 break;
220 }
Chris Lattner2d973e42005-08-18 20:07:59 +0000221 }
222 }
223
224 if (OpSlot) *OpSlot = ResultReg;
225 return ResultReg+Op.ResNo;
226}
227
228
Chris Lattnerd32b2362005-08-18 18:45:24 +0000229/// Pick a safe ordering and emit instructions for each target node in the
230/// graph.
231void SelectionDAGISel::ScheduleAndEmitDAG(SelectionDAG &SD) {
Chris Lattner068ca152005-08-18 20:11:49 +0000232 if (ViewDAGs) SD.viewGraph();
Chris Lattner620c93c2005-08-27 00:58:02 +0000233 BB = SimpleSched(SD, BB).Run();
Chris Lattnerd32b2362005-08-18 18:45:24 +0000234}