Today the front-ends (llvm-gcc and clang) generate multiple llvm.dbg.compile_units...
[oota-llvm.git] / include / llvm / CodeGen / ScheduleDAGSDNodes.h
1 //===---- llvm/CodeGen/ScheduleDAGSDNodes.h - SDNode Scheduling -*- C++ -*-===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This file implements the ScheduleDAGSDNodes class, which implements
11 // scheduling for an SDNode-based dependency graph.
12 //
13 //===----------------------------------------------------------------------===//
14
15 #ifndef LLVM_CODEGEN_SCHEDULEDAGSDNODES_H
16 #define LLVM_CODEGEN_SCHEDULEDAGSDNODES_H
17
18 #include "llvm/CodeGen/ScheduleDAG.h"
19 #include "llvm/CodeGen/SelectionDAG.h"
20 #include "llvm/ADT/SmallSet.h"
21
22 namespace llvm {
23   /// HazardRecognizer - This determines whether or not an instruction can be
24   /// issued this cycle, and whether or not a noop needs to be inserted to handle
25   /// the hazard.
26   class HazardRecognizer {
27   public:
28     virtual ~HazardRecognizer();
29     
30     enum HazardType {
31       NoHazard,      // This instruction can be emitted at this cycle.
32       Hazard,        // This instruction can't be emitted at this cycle.
33       NoopHazard     // This instruction can't be emitted, and needs noops.
34     };
35     
36     /// getHazardType - Return the hazard type of emitting this node.  There are
37     /// three possible results.  Either:
38     ///  * NoHazard: it is legal to issue this instruction on this cycle.
39     ///  * Hazard: issuing this instruction would stall the machine.  If some
40     ///     other instruction is available, issue it first.
41     ///  * NoopHazard: issuing this instruction would break the program.  If
42     ///     some other instruction can be issued, do so, otherwise issue a noop.
43     virtual HazardType getHazardType(SDNode *) {
44       return NoHazard;
45     }
46     
47     /// EmitInstruction - This callback is invoked when an instruction is
48     /// emitted, to advance the hazard state.
49     virtual void EmitInstruction(SDNode *) {}
50     
51     /// AdvanceCycle - This callback is invoked when no instructions can be
52     /// issued on this cycle without a hazard.  This should increment the
53     /// internal state of the hazard recognizer so that previously "Hazard"
54     /// instructions will now not be hazards.
55     virtual void AdvanceCycle() {}
56     
57     /// EmitNoop - This callback is invoked when a noop was added to the
58     /// instruction stream.
59     virtual void EmitNoop() {}
60   };
61
62   class ScheduleDAGSDNodes : public ScheduleDAG {
63   public:
64     SmallSet<SDNode*, 16> CommuteSet;     // Nodes that should be commuted.
65
66     ScheduleDAGSDNodes(SelectionDAG *dag, MachineBasicBlock *bb,
67                        const TargetMachine &tm);
68
69     virtual ~ScheduleDAGSDNodes() {}
70
71     /// isPassiveNode - Return true if the node is a non-scheduled leaf.
72     ///
73     static bool isPassiveNode(SDNode *Node) {
74       if (isa<ConstantSDNode>(Node))       return true;
75       if (isa<ConstantFPSDNode>(Node))     return true;
76       if (isa<RegisterSDNode>(Node))       return true;
77       if (isa<GlobalAddressSDNode>(Node))  return true;
78       if (isa<BasicBlockSDNode>(Node))     return true;
79       if (isa<FrameIndexSDNode>(Node))     return true;
80       if (isa<ConstantPoolSDNode>(Node))   return true;
81       if (isa<JumpTableSDNode>(Node))      return true;
82       if (isa<ExternalSymbolSDNode>(Node)) return true;
83       if (isa<MemOperandSDNode>(Node))     return true;
84       if (Node->getOpcode() == ISD::EntryToken) return true;
85       return false;
86     }
87
88     /// NewSUnit - Creates a new SUnit and return a ptr to it.
89     ///
90     SUnit *NewSUnit(SDNode *N) {
91       SUnits.push_back(SUnit(N, (unsigned)SUnits.size()));
92       SUnits.back().OrigNode = &SUnits.back();
93       return &SUnits.back();
94     }
95
96     /// Clone - Creates a clone of the specified SUnit. It does not copy the
97     /// predecessors / successors info nor the temporary scheduling states.
98     ///
99     SUnit *Clone(SUnit *N);
100     
101     virtual SelectionDAG *getDAG() { return DAG; }
102
103     /// BuildSchedUnits - Build SUnits from the selection dag that we are input.
104     /// This SUnit graph is similar to the SelectionDAG, but represents flagged
105     /// together nodes with a single SUnit.
106     virtual void BuildSchedUnits();
107
108     /// ComputeLatency - Compute node latency.
109     ///
110     virtual void ComputeLatency(SUnit *SU);
111
112     /// CountResults - The results of target nodes have register or immediate
113     /// operands first, then an optional chain, and optional flag operands
114     /// (which do not go into the machine instrs.)
115     static unsigned CountResults(SDNode *Node);
116
117     /// CountOperands - The inputs to target nodes have any actual inputs first,
118     /// followed by special operands that describe memory references, then an
119     /// optional chain operand, then flag operands.  Compute the number of
120     /// actual operands that will go into the resulting MachineInstr.
121     static unsigned CountOperands(SDNode *Node);
122
123     /// ComputeMemOperandsEnd - Find the index one past the last
124     /// MemOperandSDNode operand
125     static unsigned ComputeMemOperandsEnd(SDNode *Node);
126
127     /// EmitNode - Generate machine code for an node and needed dependencies.
128     /// VRBaseMap contains, for each already emitted node, the first virtual
129     /// register number for the results of the node.
130     ///
131     void EmitNode(SDNode *Node, bool IsClone,
132                   DenseMap<SDValue, unsigned> &VRBaseMap);
133     
134     virtual MachineBasicBlock *EmitSchedule();
135
136     /// Schedule - Order nodes according to selected style, filling
137     /// in the Sequence member.
138     ///
139     virtual void Schedule() = 0;
140
141     virtual void dumpNode(const SUnit *SU) const;
142
143     virtual std::string getGraphNodeLabel(const SUnit *SU) const;
144
145     virtual void getCustomGraphFeatures(GraphWriter<ScheduleDAG*> &GW) const;
146
147   private:
148     /// EmitSubregNode - Generate machine code for subreg nodes.
149     ///
150     void EmitSubregNode(SDNode *Node, 
151                         DenseMap<SDValue, unsigned> &VRBaseMap);
152
153     /// getVR - Return the virtual register corresponding to the specified result
154     /// of the specified node.
155     unsigned getVR(SDValue Op, DenseMap<SDValue, unsigned> &VRBaseMap);
156   
157     /// getDstOfCopyToRegUse - If the only use of the specified result number of
158     /// node is a CopyToReg, return its destination register. Return 0 otherwise.
159     unsigned getDstOfOnlyCopyToRegUse(SDNode *Node, unsigned ResNo) const;
160
161     void AddOperand(MachineInstr *MI, SDValue Op, unsigned IIOpNum,
162                     const TargetInstrDesc *II,
163                     DenseMap<SDValue, unsigned> &VRBaseMap);
164
165     /// EmitCopyFromReg - Generate machine code for an CopyFromReg node or an
166     /// implicit physical register output.
167     void EmitCopyFromReg(SDNode *Node, unsigned ResNo, bool IsClone,
168                          unsigned SrcReg,
169                          DenseMap<SDValue, unsigned> &VRBaseMap);
170     
171     void CreateVirtualRegisters(SDNode *Node, MachineInstr *MI,
172                                 const TargetInstrDesc &II,
173                                 DenseMap<SDValue, unsigned> &VRBaseMap);
174   };
175 }
176
177 #endif