blob: cf8d04e8a6ac79d6e8439fdd20e2c177efd0e045 [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 Lattnerda8abb02005-09-01 18:44:10 +000027#ifndef NDEBUG
Chris Lattner068ca152005-08-18 20:11:49 +000028static 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 Lattner82e14db2005-08-29 23:21:29 +0000100
Chris Lattner14b392a2005-08-24 22:02:41 +0000101 if (NodeOperands && // Ignore chain if it exists.
102 Op.getOperand(NodeOperands-1).getValueType() == MVT::Other)
103 --NodeOperands;
104
105 unsigned NumMIOperands = NodeOperands+NumResults;
Chris Lattnerda8abb02005-09-01 18:44:10 +0000106#ifndef NDEBUG
Chris Lattner14b392a2005-08-24 22:02:41 +0000107 assert((unsigned(II.numOperands) == NumMIOperands || II.numOperands == -1)&&
Chris Lattner2d973e42005-08-18 20:07:59 +0000108 "#operands for dag node doesn't match .td file!");
Chris Lattnerca6aa2f2005-08-19 01:01:34 +0000109#endif
Chris Lattner2d973e42005-08-18 20:07:59 +0000110
111 // Create the new machine instruction.
Chris Lattner14b392a2005-08-24 22:02:41 +0000112 MachineInstr *MI = new MachineInstr(Opc, NumMIOperands, true, true);
Chris Lattner2d973e42005-08-18 20:07:59 +0000113
114 // Add result register values for things that are defined by this
115 // instruction.
Chris Lattner4ccd4062005-08-19 20:45:43 +0000116 if (NumResults) {
117 // Create the result registers for this node and add the result regs to
118 // the machine instruction.
119 const TargetOperandInfo *OpInfo = II.OpInfo;
120 ResultReg = RegMap->createVirtualRegister(OpInfo[0].RegClass);
121 MI->addRegOperand(ResultReg, MachineOperand::Def);
122 for (unsigned i = 1; i != NumResults; ++i) {
123 assert(OpInfo[i].RegClass && "Isn't a register operand!");
124 MI->addRegOperand(RegMap->createVirtualRegister(OpInfo[0].RegClass),
125 MachineOperand::Def);
126 }
127 }
Chris Lattner2d973e42005-08-18 20:07:59 +0000128
Chris Lattner82e14db2005-08-29 23:21:29 +0000129 // If there is a token chain operand, emit it first, as a hack to get avoid
130 // really bad cases.
131 if (Op.getNumOperands() > NodeOperands &&
132 Op.getOperand(NodeOperands).getValueType() == MVT::Other)
133 Emit(Op.getOperand(NodeOperands));
134
135 // Emit all of the actual operands of this instruction, adding them to the
Chris Lattner2d973e42005-08-18 20:07:59 +0000136 // instruction as appropriate.
Chris Lattner82e14db2005-08-29 23:21:29 +0000137 for (unsigned i = 0; i != NodeOperands; ++i) {
Chris Lattner23553cf2005-08-22 01:04:32 +0000138 if (Op.getOperand(i).isTargetOpcode()) {
139 // Note that this case is redundant with the final else block, but we
140 // include it because it is the most common and it makes the logic
141 // simpler here.
Chris Lattner82e14db2005-08-29 23:21:29 +0000142 assert(Op.getOperand(i).getValueType() != MVT::Other &&
143 Op.getOperand(i).getValueType() != MVT::Flag &&
144 "Chain and flag operands should occur at end of operand list!");
145
146 MI->addRegOperand(Emit(Op.getOperand(i)), MachineOperand::Use);
Chris Lattner23553cf2005-08-22 01:04:32 +0000147 } else if (ConstantSDNode *C =
148 dyn_cast<ConstantSDNode>(Op.getOperand(i))) {
Chris Lattner2d973e42005-08-18 20:07:59 +0000149 MI->addZeroExtImm64Operand(C->getValue());
150 } else if (RegisterSDNode*R =dyn_cast<RegisterSDNode>(Op.getOperand(i))) {
151 MI->addRegOperand(R->getReg(), MachineOperand::Use);
Chris Lattner9b78db72005-08-19 22:38:24 +0000152 } else if (GlobalAddressSDNode *TGA =
153 dyn_cast<GlobalAddressSDNode>(Op.getOperand(i))) {
154 MI->addGlobalAddressOperand(TGA->getGlobal(), false, 0);
Chris Lattnerf85ab152005-08-21 18:49:29 +0000155 } else if (BasicBlockSDNode *BB =
156 dyn_cast<BasicBlockSDNode>(Op.getOperand(i))) {
157 MI->addMachineBasicBlockOperand(BB->getBasicBlock());
Chris Lattner81e72b12005-08-21 19:56:04 +0000158 } else if (FrameIndexSDNode *FI =
159 dyn_cast<FrameIndexSDNode>(Op.getOperand(i))) {
160 MI->addFrameIndexOperand(FI->getIndex());
Chris Lattner23553cf2005-08-22 01:04:32 +0000161 } else if (ConstantPoolSDNode *CP =
162 dyn_cast<ConstantPoolSDNode>(Op.getOperand(i))) {
Chris Lattner5839bf22005-08-26 17:15:30 +0000163 unsigned Idx = ConstPool->getConstantPoolIndex(CP->get());
164 MI->addConstantPoolIndexOperand(Idx);
Chris Lattner14b392a2005-08-24 22:02:41 +0000165 } else if (ExternalSymbolSDNode *ES =
166 dyn_cast<ExternalSymbolSDNode>(Op.getOperand(i))) {
167 MI->addExternalSymbolOperand(ES->getSymbol(), false);
Chris Lattner2d973e42005-08-18 20:07:59 +0000168 } else {
Chris Lattner82e14db2005-08-29 23:21:29 +0000169 assert(Op.getOperand(i).getValueType() != MVT::Other &&
170 Op.getOperand(i).getValueType() != MVT::Flag &&
171 "Chain and flag operands should occur at end of operand list!");
172 MI->addRegOperand(Emit(Op.getOperand(i)), MachineOperand::Use);
Chris Lattner2d973e42005-08-18 20:07:59 +0000173 }
174 }
175
Chris Lattner82e14db2005-08-29 23:21:29 +0000176 // Finally, if this node has any flag operands, we *must* emit them last, to
177 // avoid emitting operations that might clobber the flags.
178 if (Op.getNumOperands() > NodeOperands) {
179 unsigned i = NodeOperands;
180 if (Op.getOperand(i).getValueType() == MVT::Other)
181 ++i; // the chain is already selected.
182 for (; i != Op.getNumOperands(); ++i) {
183 assert(Op.getOperand(i).getValueType() == MVT::Flag &&
184 "Must be flag operands!");
185 Emit(Op.getOperand(i));
186 }
187 }
188
Chris Lattner2d973e42005-08-18 20:07:59 +0000189 // Now that we have emitted all operands, emit this instruction itself.
Chris Lattner025c39b2005-08-26 20:54:47 +0000190 if ((II.Flags & M_USES_CUSTOM_DAG_SCHED_INSERTION) == 0) {
191 BB->insert(BB->end(), MI);
192 } else {
193 // Insert this instruction into the end of the basic block, potentially
194 // taking some custom action.
195 BB = DAG.getTargetLoweringInfo().InsertAtEndOfBasicBlock(MI, BB);
196 }
Chris Lattner2d973e42005-08-18 20:07:59 +0000197 } else {
198 switch (Op.getOpcode()) {
Chris Lattnerca6aa2f2005-08-19 01:01:34 +0000199 default:
200 Op.Val->dump();
201 assert(0 && "This target-independent node should have been selected!");
Chris Lattner81e72b12005-08-21 19:56:04 +0000202 case ISD::EntryToken: break;
Chris Lattner7ef33042005-08-19 21:43:53 +0000203 case ISD::TokenFactor:
204 for (unsigned i = 0, e = Op.getNumOperands(); i != e; ++i)
205 Emit(Op.getOperand(i));
206 break;
Chris Lattnerca6aa2f2005-08-19 01:01:34 +0000207 case ISD::CopyToReg: {
Chris Lattnerf1556352005-08-30 01:58:51 +0000208 SDOperand FlagOp;
Chris Lattner55334fc2005-08-30 01:57:23 +0000209 if (Op.getNumOperands() == 4)
Chris Lattnerf1556352005-08-30 01:58:51 +0000210 FlagOp = Op.getOperand(3);
211 if (Op.getOperand(0).Val != FlagOp.Val)
Chris Lattner55334fc2005-08-30 01:57:23 +0000212 Emit(Op.getOperand(0)); // Emit the chain.
Chris Lattnerca6aa2f2005-08-19 01:01:34 +0000213 unsigned Val = Emit(Op.getOperand(2));
Chris Lattnerf1556352005-08-30 01:58:51 +0000214 if (FlagOp.Val) Emit(FlagOp);
Chris Lattner01891972005-08-19 20:50:53 +0000215 MRI.copyRegToReg(*BB, BB->end(),
216 cast<RegisterSDNode>(Op.getOperand(1))->getReg(), Val,
217 RegMap->getRegClass(Val));
Chris Lattnerca6aa2f2005-08-19 01:01:34 +0000218 break;
219 }
Chris Lattner7ef33042005-08-19 21:43:53 +0000220 case ISD::CopyFromReg: {
221 Emit(Op.getOperand(0)); // Emit the chain.
222 unsigned SrcReg = cast<RegisterSDNode>(Op.getOperand(1))->getReg();
223
224 // Figure out the register class to create for the destreg.
Chris Lattnerfe0c2c82005-08-20 18:07:27 +0000225 const TargetRegisterClass *TRC = 0;
Chris Lattner7ef33042005-08-19 21:43:53 +0000226 if (MRegisterInfo::isVirtualRegister(SrcReg)) {
227 TRC = RegMap->getRegClass(SrcReg);
228 } else {
229 // FIXME: we don't know what register class to generate this for. Do
230 // a brute force search and pick the first match. :(
231 for (MRegisterInfo::regclass_iterator I = MRI.regclass_begin(),
232 E = MRI.regclass_end(); I != E; ++I)
233 if ((*I)->contains(SrcReg)) {
234 TRC = *I;
235 break;
236 }
237 assert(TRC && "Couldn't find register class for reg copy!");
238 }
239
240 // Create the reg, emit the copy.
241 ResultReg = RegMap->createVirtualRegister(TRC);
242 MRI.copyRegToReg(*BB, BB->end(), ResultReg, SrcReg, TRC);
243 break;
244 }
Chris Lattner2d973e42005-08-18 20:07:59 +0000245 }
246 }
247
248 if (OpSlot) *OpSlot = ResultReg;
249 return ResultReg+Op.ResNo;
250}
251
252
Chris Lattnerd32b2362005-08-18 18:45:24 +0000253/// Pick a safe ordering and emit instructions for each target node in the
254/// graph.
255void SelectionDAGISel::ScheduleAndEmitDAG(SelectionDAG &SD) {
Chris Lattner068ca152005-08-18 20:11:49 +0000256 if (ViewDAGs) SD.viewGraph();
Chris Lattner620c93c2005-08-27 00:58:02 +0000257 BB = SimpleSched(SD, BB).Run();
Chris Lattnerd32b2362005-08-18 18:45:24 +0000258}