blob: d77578e28666c8d881e020adde4c2ce02cb9e615 [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();
83 if (NumResults && Op.getOperand(NumResults-1).getValueType() == MVT::Other)
84 --NumResults;
Chris Lattnerca6aa2f2005-08-19 01:01:34 +000085#ifndef _NDEBUG
86 unsigned Operands = Op.getNumOperands();
87 if (Operands && Op.getOperand(Operands-1).getValueType() == MVT::Other)
88 --Operands;
Chris Lattner4ccd4062005-08-19 20:45:43 +000089 assert(unsigned(II.numOperands) == Operands+NumResults &&
Chris Lattner2d973e42005-08-18 20:07:59 +000090 "#operands for dag node doesn't match .td file!");
Chris Lattnerca6aa2f2005-08-19 01:01:34 +000091#endif
Chris Lattner2d973e42005-08-18 20:07:59 +000092
93 // Create the new machine instruction.
94 MachineInstr *MI = new MachineInstr(Opc, II.numOperands, true, true);
95
96 // Add result register values for things that are defined by this
97 // instruction.
Chris Lattner4ccd4062005-08-19 20:45:43 +000098 if (NumResults) {
99 // Create the result registers for this node and add the result regs to
100 // the machine instruction.
101 const TargetOperandInfo *OpInfo = II.OpInfo;
102 ResultReg = RegMap->createVirtualRegister(OpInfo[0].RegClass);
103 MI->addRegOperand(ResultReg, MachineOperand::Def);
104 for (unsigned i = 1; i != NumResults; ++i) {
105 assert(OpInfo[i].RegClass && "Isn't a register operand!");
106 MI->addRegOperand(RegMap->createVirtualRegister(OpInfo[0].RegClass),
107 MachineOperand::Def);
108 }
109 }
Chris Lattner2d973e42005-08-18 20:07:59 +0000110
111 // Emit all of the operands of this instruction, adding them to the
112 // instruction as appropriate.
113 for (unsigned i = 0, e = Op.getNumOperands(); i != e; ++i) {
114 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(i))) {
115 MI->addZeroExtImm64Operand(C->getValue());
116 } else if (RegisterSDNode*R =dyn_cast<RegisterSDNode>(Op.getOperand(i))) {
117 MI->addRegOperand(R->getReg(), MachineOperand::Use);
118 } else {
119 unsigned R = Emit(Op.getOperand(i));
120 // Add an operand, unless this corresponds to a chain node.
121 if (Op.getOperand(i).getValueType() != MVT::Other)
122 MI->addRegOperand(R, MachineOperand::Use);
123 }
124 }
125
126 // Now that we have emitted all operands, emit this instruction itself.
127 BB->insert(BB->end(), MI);
128 } else {
129 switch (Op.getOpcode()) {
Chris Lattnerca6aa2f2005-08-19 01:01:34 +0000130 default:
131 Op.Val->dump();
132 assert(0 && "This target-independent node should have been selected!");
Chris Lattner2d973e42005-08-18 20:07:59 +0000133 case ISD::EntryToken: break;
Chris Lattnerca6aa2f2005-08-19 01:01:34 +0000134 case ISD::CopyToReg: {
135 unsigned Val = Emit(Op.getOperand(2));
Chris Lattner01891972005-08-19 20:50:53 +0000136 MRI.copyRegToReg(*BB, BB->end(),
137 cast<RegisterSDNode>(Op.getOperand(1))->getReg(), Val,
138 RegMap->getRegClass(Val));
Chris Lattnerca6aa2f2005-08-19 01:01:34 +0000139 break;
140 }
Chris Lattner2d973e42005-08-18 20:07:59 +0000141 }
142 }
143
144 if (OpSlot) *OpSlot = ResultReg;
145 return ResultReg+Op.ResNo;
146}
147
148
Chris Lattnerd32b2362005-08-18 18:45:24 +0000149/// Pick a safe ordering and emit instructions for each target node in the
150/// graph.
151void SelectionDAGISel::ScheduleAndEmitDAG(SelectionDAG &SD) {
Chris Lattner068ca152005-08-18 20:11:49 +0000152 if (ViewDAGs) SD.viewGraph();
Chris Lattner2d973e42005-08-18 20:07:59 +0000153 SimpleSched(SD, BB).Run();
Chris Lattnerd32b2362005-08-18 18:45:24 +0000154}