blob: 036023e049581ca6a354c191e5b0611d0f2f8ff0 [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 Lattner4ccd4062005-08-19 20:45:43 +000016#include "llvm/CodeGen/MachineFunction.h"
Chris Lattnerd32b2362005-08-18 18:45:24 +000017#include "llvm/CodeGen/SelectionDAGISel.h"
Chris Lattner2d973e42005-08-18 20:07:59 +000018#include "llvm/CodeGen/SelectionDAG.h"
Chris Lattner4ccd4062005-08-19 20:45:43 +000019#include "llvm/CodeGen/SSARegMap.h"
Chris Lattner2d973e42005-08-18 20:07:59 +000020#include "llvm/Target/TargetMachine.h"
21#include "llvm/Target/TargetInstrInfo.h"
Chris Lattner068ca152005-08-18 20:11:49 +000022#include "llvm/Support/CommandLine.h"
Chris Lattnerd32b2362005-08-18 18:45:24 +000023using namespace llvm;
24
Chris Lattner068ca152005-08-18 20:11:49 +000025#ifndef _NDEBUG
26static cl::opt<bool>
27ViewDAGs("view-sched-dags", cl::Hidden,
28 cl::desc("Pop up a window to show sched dags as they are processed"));
29#else
30static const bool ViewDAGS = 0;
31#endif
32
Chris Lattner2d973e42005-08-18 20:07:59 +000033namespace {
34 class SimpleSched {
35 SelectionDAG &DAG;
36 MachineBasicBlock *BB;
37 const TargetMachine &TM;
38 const TargetInstrInfo &TII;
Chris Lattner01891972005-08-19 20:50:53 +000039 const MRegisterInfo &MRI;
Chris Lattner4ccd4062005-08-19 20:45:43 +000040 SSARegMap *RegMap;
Chris Lattner2d973e42005-08-18 20:07:59 +000041
42 std::map<SDNode *, unsigned> EmittedOps;
43 public:
44 SimpleSched(SelectionDAG &D, MachineBasicBlock *bb)
Chris Lattner4ccd4062005-08-19 20:45:43 +000045 : DAG(D), BB(bb), TM(D.getTarget()), TII(*TM.getInstrInfo()),
Chris Lattner01891972005-08-19 20:50:53 +000046 MRI(*TM.getRegisterInfo()), RegMap(BB->getParent()->getSSARegMap()) {
Chris Lattner2d973e42005-08-18 20:07:59 +000047 assert(&TII && "Target doesn't provide instr info?");
Chris Lattner01891972005-08-19 20:50:53 +000048 assert(&MRI && "Target doesn't provide register info?");
Chris Lattner2d973e42005-08-18 20:07:59 +000049 }
50
51 void Run() {
52 Emit(DAG.getRoot());
53 }
54
55 private:
56 unsigned Emit(SDOperand Op);
57 };
58}
59
60unsigned SimpleSched::Emit(SDOperand Op) {
61 // Check to see if we have already emitted this. If so, return the value
62 // already emitted. Note that if a node has a single use it cannot be
63 // revisited, so don't bother putting it in the map.
64 unsigned *OpSlot;
65 if (Op.Val->hasOneUse()) {
66 OpSlot = 0; // No reuse possible.
67 } else {
68 std::map<SDNode *, unsigned>::iterator OpI = EmittedOps.lower_bound(Op.Val);
69 if (OpI != EmittedOps.end() && OpI->first == Op.Val)
70 return OpI->second + Op.ResNo;
71 OpSlot = &EmittedOps.insert(OpI, std::make_pair(Op.Val, 0))->second;
72 }
73
74 unsigned ResultReg = 0;
75 if (Op.isTargetOpcode()) {
76 unsigned Opc = Op.getTargetOpcode();
77 const TargetInstrDescriptor &II = TII.get(Opc);
78
79 // Target nodes have any register or immediate operands before any chain
80 // nodes. Check that the DAG matches the TD files's expectation of #
81 // operands.
Chris Lattner4ccd4062005-08-19 20:45:43 +000082 unsigned NumResults = Op.Val->getNumValues();
Chris Lattnerf85ab152005-08-21 18:49:29 +000083 if (NumResults && Op.Val->getValueType(NumResults-1) == MVT::Other)
Chris Lattner4ccd4062005-08-19 20:45:43 +000084 --NumResults;
Chris Lattner14b392a2005-08-24 22:02:41 +000085
86 unsigned NodeOperands = Op.getNumOperands();
87 if (NodeOperands && // Ignore chain if it exists.
88 Op.getOperand(NodeOperands-1).getValueType() == MVT::Other)
89 --NodeOperands;
90
91 unsigned NumMIOperands = NodeOperands+NumResults;
Chris Lattnerca6aa2f2005-08-19 01:01:34 +000092#ifndef _NDEBUG
Chris Lattner14b392a2005-08-24 22:02:41 +000093 assert((unsigned(II.numOperands) == NumMIOperands || II.numOperands == -1)&&
Chris Lattner2d973e42005-08-18 20:07:59 +000094 "#operands for dag node doesn't match .td file!");
Chris Lattnerca6aa2f2005-08-19 01:01:34 +000095#endif
Chris Lattner2d973e42005-08-18 20:07:59 +000096
97 // Create the new machine instruction.
Chris Lattner14b392a2005-08-24 22:02:41 +000098 MachineInstr *MI = new MachineInstr(Opc, NumMIOperands, true, true);
Chris Lattner2d973e42005-08-18 20:07:59 +000099
100 // Add result register values for things that are defined by this
101 // instruction.
Chris Lattner4ccd4062005-08-19 20:45:43 +0000102 if (NumResults) {
103 // Create the result registers for this node and add the result regs to
104 // the machine instruction.
105 const TargetOperandInfo *OpInfo = II.OpInfo;
106 ResultReg = RegMap->createVirtualRegister(OpInfo[0].RegClass);
107 MI->addRegOperand(ResultReg, MachineOperand::Def);
108 for (unsigned i = 1; i != NumResults; ++i) {
109 assert(OpInfo[i].RegClass && "Isn't a register operand!");
110 MI->addRegOperand(RegMap->createVirtualRegister(OpInfo[0].RegClass),
111 MachineOperand::Def);
112 }
113 }
Chris Lattner2d973e42005-08-18 20:07:59 +0000114
115 // Emit all of the operands of this instruction, adding them to the
116 // instruction as appropriate.
117 for (unsigned i = 0, e = Op.getNumOperands(); i != e; ++i) {
Chris Lattner23553cf2005-08-22 01:04:32 +0000118 if (Op.getOperand(i).isTargetOpcode()) {
119 // Note that this case is redundant with the final else block, but we
120 // include it because it is the most common and it makes the logic
121 // simpler here.
122 unsigned R = Emit(Op.getOperand(i));
123 // Add an operand, unless this corresponds to a chain node.
124 if (Op.getOperand(i).getValueType() != MVT::Other)
125 MI->addRegOperand(R, MachineOperand::Use);
126 } else if (ConstantSDNode *C =
127 dyn_cast<ConstantSDNode>(Op.getOperand(i))) {
Chris Lattner2d973e42005-08-18 20:07:59 +0000128 MI->addZeroExtImm64Operand(C->getValue());
129 } else if (RegisterSDNode*R =dyn_cast<RegisterSDNode>(Op.getOperand(i))) {
130 MI->addRegOperand(R->getReg(), MachineOperand::Use);
Chris Lattner9b78db72005-08-19 22:38:24 +0000131 } else if (GlobalAddressSDNode *TGA =
132 dyn_cast<GlobalAddressSDNode>(Op.getOperand(i))) {
133 MI->addGlobalAddressOperand(TGA->getGlobal(), false, 0);
Chris Lattnerf85ab152005-08-21 18:49:29 +0000134 } else if (BasicBlockSDNode *BB =
135 dyn_cast<BasicBlockSDNode>(Op.getOperand(i))) {
136 MI->addMachineBasicBlockOperand(BB->getBasicBlock());
Chris Lattner81e72b12005-08-21 19:56:04 +0000137 } else if (FrameIndexSDNode *FI =
138 dyn_cast<FrameIndexSDNode>(Op.getOperand(i))) {
139 MI->addFrameIndexOperand(FI->getIndex());
Chris Lattner23553cf2005-08-22 01:04:32 +0000140 } else if (ConstantPoolSDNode *CP =
141 dyn_cast<ConstantPoolSDNode>(Op.getOperand(i))) {
142 MI->addConstantPoolIndexOperand(CP->getIndex());
Chris Lattner14b392a2005-08-24 22:02:41 +0000143 } else if (ExternalSymbolSDNode *ES =
144 dyn_cast<ExternalSymbolSDNode>(Op.getOperand(i))) {
145 MI->addExternalSymbolOperand(ES->getSymbol(), false);
Chris Lattner2d973e42005-08-18 20:07:59 +0000146 } else {
147 unsigned R = Emit(Op.getOperand(i));
148 // Add an operand, unless this corresponds to a chain node.
149 if (Op.getOperand(i).getValueType() != MVT::Other)
150 MI->addRegOperand(R, MachineOperand::Use);
151 }
152 }
153
154 // Now that we have emitted all operands, emit this instruction itself.
155 BB->insert(BB->end(), MI);
156 } else {
157 switch (Op.getOpcode()) {
Chris Lattnerca6aa2f2005-08-19 01:01:34 +0000158 default:
159 Op.Val->dump();
160 assert(0 && "This target-independent node should have been selected!");
Chris Lattner81e72b12005-08-21 19:56:04 +0000161 case ISD::EntryToken: break;
Chris Lattner7ef33042005-08-19 21:43:53 +0000162 case ISD::TokenFactor:
163 for (unsigned i = 0, e = Op.getNumOperands(); i != e; ++i)
164 Emit(Op.getOperand(i));
165 break;
Chris Lattnerca6aa2f2005-08-19 01:01:34 +0000166 case ISD::CopyToReg: {
Chris Lattner7ef33042005-08-19 21:43:53 +0000167 Emit(Op.getOperand(0)); // Emit the chain.
Chris Lattnerca6aa2f2005-08-19 01:01:34 +0000168 unsigned Val = Emit(Op.getOperand(2));
Chris Lattner01891972005-08-19 20:50:53 +0000169 MRI.copyRegToReg(*BB, BB->end(),
170 cast<RegisterSDNode>(Op.getOperand(1))->getReg(), Val,
171 RegMap->getRegClass(Val));
Chris Lattnerca6aa2f2005-08-19 01:01:34 +0000172 break;
173 }
Chris Lattner7ef33042005-08-19 21:43:53 +0000174 case ISD::CopyFromReg: {
175 Emit(Op.getOperand(0)); // Emit the chain.
176 unsigned SrcReg = cast<RegisterSDNode>(Op.getOperand(1))->getReg();
177
178 // Figure out the register class to create for the destreg.
Chris Lattnerfe0c2c82005-08-20 18:07:27 +0000179 const TargetRegisterClass *TRC = 0;
Chris Lattner7ef33042005-08-19 21:43:53 +0000180 if (MRegisterInfo::isVirtualRegister(SrcReg)) {
181 TRC = RegMap->getRegClass(SrcReg);
182 } else {
183 // FIXME: we don't know what register class to generate this for. Do
184 // a brute force search and pick the first match. :(
185 for (MRegisterInfo::regclass_iterator I = MRI.regclass_begin(),
186 E = MRI.regclass_end(); I != E; ++I)
187 if ((*I)->contains(SrcReg)) {
188 TRC = *I;
189 break;
190 }
191 assert(TRC && "Couldn't find register class for reg copy!");
192 }
193
194 // Create the reg, emit the copy.
195 ResultReg = RegMap->createVirtualRegister(TRC);
196 MRI.copyRegToReg(*BB, BB->end(), ResultReg, SrcReg, TRC);
197 break;
198 }
Chris Lattner2d973e42005-08-18 20:07:59 +0000199 }
200 }
201
202 if (OpSlot) *OpSlot = ResultReg;
203 return ResultReg+Op.ResNo;
204}
205
206
Chris Lattnerd32b2362005-08-18 18:45:24 +0000207/// Pick a safe ordering and emit instructions for each target node in the
208/// graph.
209void SelectionDAGISel::ScheduleAndEmitDAG(SelectionDAG &SD) {
Chris Lattner068ca152005-08-18 20:11:49 +0000210 if (ViewDAGs) SD.viewGraph();
Chris Lattner2d973e42005-08-18 20:07:59 +0000211 SimpleSched(SD, BB).Run();
Chris Lattnerd32b2362005-08-18 18:45:24 +0000212}