Remove unused field.
[oota-llvm.git] / include / llvm / CodeGen / ScheduleDAGInstrs.h
1 //==- ScheduleDAGInstrs.h - MachineInstr 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 ScheduleDAGInstrs class, which implements
11 // scheduling for a MachineInstr-based dependency graph.
12 //
13 //===----------------------------------------------------------------------===//
14
15 #ifndef LLVM_CODEGEN_SCHEDULEDAGINSTRS_H
16 #define LLVM_CODEGEN_SCHEDULEDAGINSTRS_H
17
18 #include "llvm/ADT/SparseSet.h"
19 #include "llvm/ADT/SparseMultiSet.h"
20 #include "llvm/CodeGen/ScheduleDAG.h"
21 #include "llvm/CodeGen/TargetSchedule.h"
22 #include "llvm/Support/Compiler.h"
23 #include "llvm/Target/TargetRegisterInfo.h"
24
25 namespace llvm {
26   class MachineFrameInfo;
27   class MachineLoopInfo;
28   class MachineDominatorTree;
29   class LiveIntervals;
30   class RegPressureTracker;
31
32   /// An individual mapping from virtual register number to SUnit.
33   struct VReg2SUnit {
34     unsigned VirtReg;
35     SUnit *SU;
36
37     VReg2SUnit(unsigned reg, SUnit *su): VirtReg(reg), SU(su) {}
38
39     unsigned getSparseSetIndex() const {
40       return TargetRegisterInfo::virtReg2Index(VirtReg);
41     }
42   };
43
44   /// Record a physical register access.
45   /// For non data-dependent uses, OpIdx == -1.
46   struct PhysRegSUOper {
47     SUnit *SU;
48     int OpIdx;
49     unsigned Reg;
50
51     PhysRegSUOper(SUnit *su, int op, unsigned R): SU(su), OpIdx(op), Reg(R) {}
52
53     unsigned getSparseSetIndex() const { return Reg; }
54   };
55
56   /// Use a SparseMultiSet to track physical registers. Storage is only
57   /// allocated once for the pass. It can be cleared in constant time and reused
58   /// without any frees.
59   typedef SparseMultiSet<PhysRegSUOper, llvm::identity<unsigned>, uint16_t> Reg2SUnitsMap;
60
61   /// Use SparseSet as a SparseMap by relying on the fact that it never
62   /// compares ValueT's, only unsigned keys. This allows the set to be cleared
63   /// between scheduling regions in constant time as long as ValueT does not
64   /// require a destructor.
65   typedef SparseSet<VReg2SUnit, VirtReg2IndexFunctor> VReg2SUnitMap;
66
67   /// ScheduleDAGInstrs - A ScheduleDAG subclass for scheduling lists of
68   /// MachineInstrs.
69   class ScheduleDAGInstrs : public ScheduleDAG {
70   protected:
71     const MachineLoopInfo &MLI;
72     const MachineDominatorTree &MDT;
73     const MachineFrameInfo *MFI;
74
75     /// Live Intervals provides reaching defs in preRA scheduling.
76     LiveIntervals *LIS;
77
78     /// TargetSchedModel provides an interface to the machine model.
79     TargetSchedModel SchedModel;
80
81     /// isPostRA flag indicates vregs cannot be present.
82     bool IsPostRA;
83
84     /// The standard DAG builder does not normally include terminators as DAG
85     /// nodes because it does not create the necessary dependencies to prevent
86     /// reordering. A specialized scheduler can overide
87     /// TargetInstrInfo::isSchedulingBoundary then enable this flag to indicate
88     /// it has taken responsibility for scheduling the terminator correctly.
89     bool CanHandleTerminators;
90
91     /// State specific to the current scheduling region.
92     /// ------------------------------------------------
93
94     /// The block in which to insert instructions
95     MachineBasicBlock *BB;
96
97     /// The beginning of the range to be scheduled.
98     MachineBasicBlock::iterator RegionBegin;
99
100     /// The end of the range to be scheduled.
101     MachineBasicBlock::iterator RegionEnd;
102
103     /// Instructions in this region (distance(RegionBegin, RegionEnd)).
104     unsigned NumRegionInstrs;
105
106     /// After calling BuildSchedGraph, each machine instruction in the current
107     /// scheduling region is mapped to an SUnit.
108     DenseMap<MachineInstr*, SUnit*> MISUnitMap;
109
110     /// State internal to DAG building.
111     /// -------------------------------
112
113     /// Defs, Uses - Remember where defs and uses of each register are as we
114     /// iterate upward through the instructions. This is allocated here instead
115     /// of inside BuildSchedGraph to avoid the need for it to be initialized and
116     /// destructed for each block.
117     Reg2SUnitsMap Defs;
118     Reg2SUnitsMap Uses;
119
120     /// Track the last instructon in this region defining each virtual register.
121     VReg2SUnitMap VRegDefs;
122
123     /// PendingLoads - Remember where unknown loads are after the most recent
124     /// unknown store, as we iterate. As with Defs and Uses, this is here
125     /// to minimize construction/destruction.
126     std::vector<SUnit *> PendingLoads;
127
128     /// DbgValues - Remember instruction that precedes DBG_VALUE.
129     /// These are generated by buildSchedGraph but persist so they can be
130     /// referenced when emitting the final schedule.
131     typedef std::vector<std::pair<MachineInstr *, MachineInstr *> >
132       DbgValueVector;
133     DbgValueVector DbgValues;
134     MachineInstr *FirstDbgValue;
135
136   public:
137     explicit ScheduleDAGInstrs(MachineFunction &mf,
138                                const MachineLoopInfo &mli,
139                                const MachineDominatorTree &mdt,
140                                bool IsPostRAFlag,
141                                LiveIntervals *LIS = 0);
142
143     virtual ~ScheduleDAGInstrs() {}
144
145     /// \brief Expose LiveIntervals for use in DAG mutators and such.
146     LiveIntervals *getLIS() const { return LIS; }
147
148     /// \brief Get the machine model for instruction scheduling.
149     const TargetSchedModel *getSchedModel() const { return &SchedModel; }
150
151     /// \brief Resolve and cache a resolved scheduling class for an SUnit.
152     const MCSchedClassDesc *getSchedClass(SUnit *SU) const {
153       if (!SU->SchedClass && SchedModel.hasInstrSchedModel())
154         SU->SchedClass = SchedModel.resolveSchedClass(SU->getInstr());
155       return SU->SchedClass;
156     }
157
158     /// begin - Return an iterator to the top of the current scheduling region.
159     MachineBasicBlock::iterator begin() const { return RegionBegin; }
160
161     /// end - Return an iterator to the bottom of the current scheduling region.
162     MachineBasicBlock::iterator end() const { return RegionEnd; }
163
164     /// newSUnit - Creates a new SUnit and return a ptr to it.
165     SUnit *newSUnit(MachineInstr *MI);
166
167     /// getSUnit - Return an existing SUnit for this MI, or NULL.
168     SUnit *getSUnit(MachineInstr *MI) const;
169
170     /// startBlock - Prepare to perform scheduling in the given block.
171     virtual void startBlock(MachineBasicBlock *BB);
172
173     /// finishBlock - Clean up after scheduling in the given block.
174     virtual void finishBlock();
175
176     /// Initialize the scheduler state for the next scheduling region.
177     virtual void enterRegion(MachineBasicBlock *bb,
178                              MachineBasicBlock::iterator begin,
179                              MachineBasicBlock::iterator end,
180                              unsigned regioninstrs);
181
182     /// Notify that the scheduler has finished scheduling the current region.
183     virtual void exitRegion();
184
185     /// buildSchedGraph - Build SUnits from the MachineBasicBlock that we are
186     /// input.
187     void buildSchedGraph(AliasAnalysis *AA, RegPressureTracker *RPTracker = 0);
188
189     /// addSchedBarrierDeps - Add dependencies from instructions in the current
190     /// list of instructions being scheduled to scheduling barrier. We want to
191     /// make sure instructions which define registers that are either used by
192     /// the terminator or are live-out are properly scheduled. This is
193     /// especially important when the definition latency of the return value(s)
194     /// are too high to be hidden by the branch or when the liveout registers
195     /// used by instructions in the fallthrough block.
196     void addSchedBarrierDeps();
197
198     /// schedule - Order nodes according to selected style, filling
199     /// in the Sequence member.
200     ///
201     /// Typically, a scheduling algorithm will implement schedule() without
202     /// overriding enterRegion() or exitRegion().
203     virtual void schedule() = 0;
204
205     /// finalizeSchedule - Allow targets to perform final scheduling actions at
206     /// the level of the whole MachineFunction. By default does nothing.
207     virtual void finalizeSchedule() {}
208
209     virtual void dumpNode(const SUnit *SU) const;
210
211     /// Return a label for a DAG node that points to an instruction.
212     virtual std::string getGraphNodeLabel(const SUnit *SU) const;
213
214     /// Return a label for the region of code covered by the DAG.
215     virtual std::string getDAGName() const;
216
217   protected:
218     void initSUnits();
219     void addPhysRegDataDeps(SUnit *SU, unsigned OperIdx);
220     void addPhysRegDeps(SUnit *SU, unsigned OperIdx);
221     void addVRegDefDeps(SUnit *SU, unsigned OperIdx);
222     void addVRegUseDeps(SUnit *SU, unsigned OperIdx);
223   };
224
225   /// newSUnit - Creates a new SUnit and return a ptr to it.
226   inline SUnit *ScheduleDAGInstrs::newSUnit(MachineInstr *MI) {
227 #ifndef NDEBUG
228     const SUnit *Addr = SUnits.empty() ? 0 : &SUnits[0];
229 #endif
230     SUnits.push_back(SUnit(MI, (unsigned)SUnits.size()));
231     assert((Addr == 0 || Addr == &SUnits[0]) &&
232            "SUnits std::vector reallocated on the fly!");
233     SUnits.back().OrigNode = &SUnits.back();
234     return &SUnits.back();
235   }
236
237   /// getSUnit - Return an existing SUnit for this MI, or NULL.
238   inline SUnit *ScheduleDAGInstrs::getSUnit(MachineInstr *MI) const {
239     DenseMap<MachineInstr*, SUnit*>::const_iterator I = MISUnitMap.find(MI);
240     if (I == MISUnitMap.end())
241       return 0;
242     return I->second;
243   }
244 } // namespace llvm
245
246 #endif