blob: 87943f9253d68d06678fbe541e891afbf806d2e7 [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 Lattner4ccd4062005-08-19 20:45:43 +000039 SSARegMap *RegMap;
Chris Lattner2d973e42005-08-18 20:07:59 +000040
41 std::map<SDNode *, unsigned> EmittedOps;
42 public:
43 SimpleSched(SelectionDAG &D, MachineBasicBlock *bb)
Chris Lattner4ccd4062005-08-19 20:45:43 +000044 : DAG(D), BB(bb), TM(D.getTarget()), TII(*TM.getInstrInfo()),
45 RegMap(BB->getParent()->getSSARegMap()) {
Chris Lattner2d973e42005-08-18 20:07:59 +000046 assert(&TII && "Target doesn't provide instr info?");
47 }
48
49 void Run() {
50 Emit(DAG.getRoot());
51 }
52
53 private:
54 unsigned Emit(SDOperand Op);
55 };
56}
57
58unsigned SimpleSched::Emit(SDOperand Op) {
59 // Check to see if we have already emitted this. If so, return the value
60 // already emitted. Note that if a node has a single use it cannot be
61 // revisited, so don't bother putting it in the map.
62 unsigned *OpSlot;
63 if (Op.Val->hasOneUse()) {
64 OpSlot = 0; // No reuse possible.
65 } else {
66 std::map<SDNode *, unsigned>::iterator OpI = EmittedOps.lower_bound(Op.Val);
67 if (OpI != EmittedOps.end() && OpI->first == Op.Val)
68 return OpI->second + Op.ResNo;
69 OpSlot = &EmittedOps.insert(OpI, std::make_pair(Op.Val, 0))->second;
70 }
71
72 unsigned ResultReg = 0;
73 if (Op.isTargetOpcode()) {
74 unsigned Opc = Op.getTargetOpcode();
75 const TargetInstrDescriptor &II = TII.get(Opc);
76
77 // Target nodes have any register or immediate operands before any chain
78 // nodes. Check that the DAG matches the TD files's expectation of #
79 // operands.
Chris Lattner4ccd4062005-08-19 20:45:43 +000080 unsigned NumResults = Op.Val->getNumValues();
81 if (NumResults && Op.getOperand(NumResults-1).getValueType() == MVT::Other)
82 --NumResults;
Chris Lattnerca6aa2f2005-08-19 01:01:34 +000083#ifndef _NDEBUG
84 unsigned Operands = Op.getNumOperands();
85 if (Operands && Op.getOperand(Operands-1).getValueType() == MVT::Other)
86 --Operands;
Chris Lattner4ccd4062005-08-19 20:45:43 +000087 assert(unsigned(II.numOperands) == Operands+NumResults &&
Chris Lattner2d973e42005-08-18 20:07:59 +000088 "#operands for dag node doesn't match .td file!");
Chris Lattnerca6aa2f2005-08-19 01:01:34 +000089#endif
Chris Lattner2d973e42005-08-18 20:07:59 +000090
91 // Create the new machine instruction.
92 MachineInstr *MI = new MachineInstr(Opc, II.numOperands, true, true);
93
94 // Add result register values for things that are defined by this
95 // instruction.
Chris Lattner4ccd4062005-08-19 20:45:43 +000096 if (NumResults) {
97 // Create the result registers for this node and add the result regs to
98 // the machine instruction.
99 const TargetOperandInfo *OpInfo = II.OpInfo;
100 ResultReg = RegMap->createVirtualRegister(OpInfo[0].RegClass);
101 MI->addRegOperand(ResultReg, MachineOperand::Def);
102 for (unsigned i = 1; i != NumResults; ++i) {
103 assert(OpInfo[i].RegClass && "Isn't a register operand!");
104 MI->addRegOperand(RegMap->createVirtualRegister(OpInfo[0].RegClass),
105 MachineOperand::Def);
106 }
107 }
Chris Lattner2d973e42005-08-18 20:07:59 +0000108
109 // Emit all of the operands of this instruction, adding them to the
110 // instruction as appropriate.
111 for (unsigned i = 0, e = Op.getNumOperands(); i != e; ++i) {
112 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(i))) {
113 MI->addZeroExtImm64Operand(C->getValue());
114 } else if (RegisterSDNode*R =dyn_cast<RegisterSDNode>(Op.getOperand(i))) {
115 MI->addRegOperand(R->getReg(), MachineOperand::Use);
116 } else {
117 unsigned R = Emit(Op.getOperand(i));
118 // Add an operand, unless this corresponds to a chain node.
119 if (Op.getOperand(i).getValueType() != MVT::Other)
120 MI->addRegOperand(R, MachineOperand::Use);
121 }
122 }
123
124 // Now that we have emitted all operands, emit this instruction itself.
125 BB->insert(BB->end(), MI);
126 } else {
127 switch (Op.getOpcode()) {
Chris Lattnerca6aa2f2005-08-19 01:01:34 +0000128 default:
129 Op.Val->dump();
130 assert(0 && "This target-independent node should have been selected!");
Chris Lattner2d973e42005-08-18 20:07:59 +0000131 case ISD::EntryToken: break;
Chris Lattnerca6aa2f2005-08-19 01:01:34 +0000132 case ISD::CopyToReg: {
133 unsigned Val = Emit(Op.getOperand(2));
134 // FIXME: DO THE COPY NOW.
135 break;
136 }
Chris Lattner2d973e42005-08-18 20:07:59 +0000137 }
138 }
139
140 if (OpSlot) *OpSlot = ResultReg;
141 return ResultReg+Op.ResNo;
142}
143
144
Chris Lattnerd32b2362005-08-18 18:45:24 +0000145/// Pick a safe ordering and emit instructions for each target node in the
146/// graph.
147void SelectionDAGISel::ScheduleAndEmitDAG(SelectionDAG &SD) {
Chris Lattner068ca152005-08-18 20:11:49 +0000148 if (ViewDAGs) SD.viewGraph();
Chris Lattner2d973e42005-08-18 20:07:59 +0000149 SimpleSched(SD, BB).Run();
Chris Lattnerd32b2362005-08-18 18:45:24 +0000150}