1 //==- ScheduleDAGInstrs.h - MachineInstr Scheduling --------------*- C++ -*-==//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file implements the ScheduleDAGInstrs class, which implements
11 // scheduling for a MachineInstr-based dependency graph.
13 //===----------------------------------------------------------------------===//
15 #ifndef LLVM_CODEGEN_SCHEDULEDAGINSTRS_H
16 #define LLVM_CODEGEN_SCHEDULEDAGINSTRS_H
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"
26 class MachineFrameInfo;
27 class MachineLoopInfo;
28 class MachineDominatorTree;
30 class RegPressureTracker;
32 /// An individual mapping from virtual register number to SUnit.
37 VReg2SUnit(unsigned reg, SUnit *su): VirtReg(reg), SU(su) {}
39 unsigned getSparseSetIndex() const {
40 return TargetRegisterInfo::virtReg2Index(VirtReg);
44 /// Record a physical register access.
45 /// For non data-dependent uses, OpIdx == -1.
46 struct PhysRegSUOper {
51 PhysRegSUOper(SUnit *su, int op, unsigned R): SU(su), OpIdx(op), Reg(R) {}
53 unsigned getSparseSetIndex() const { return Reg; }
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;
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;
67 /// ScheduleDAGInstrs - A ScheduleDAG subclass for scheduling lists of
69 class ScheduleDAGInstrs : public ScheduleDAG {
71 const MachineLoopInfo &MLI;
72 const MachineDominatorTree &MDT;
73 const MachineFrameInfo *MFI;
75 /// Live Intervals provides reaching defs in preRA scheduling.
78 /// TargetSchedModel provides an interface to the machine model.
79 TargetSchedModel SchedModel;
81 /// isPostRA flag indicates vregs cannot be present.
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;
91 /// State specific to the current scheduling region.
92 /// ------------------------------------------------
94 /// The block in which to insert instructions
95 MachineBasicBlock *BB;
97 /// The beginning of the range to be scheduled.
98 MachineBasicBlock::iterator RegionBegin;
100 /// The end of the range to be scheduled.
101 MachineBasicBlock::iterator RegionEnd;
103 /// Instructions in this region (distance(RegionBegin, RegionEnd)).
104 unsigned NumRegionInstrs;
106 /// After calling BuildSchedGraph, each machine instruction in the current
107 /// scheduling region is mapped to an SUnit.
108 DenseMap<MachineInstr*, SUnit*> MISUnitMap;
110 /// State internal to DAG building.
111 /// -------------------------------
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.
120 /// Track the last instructon in this region defining each virtual register.
121 VReg2SUnitMap VRegDefs;
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;
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 *> >
133 DbgValueVector DbgValues;
134 MachineInstr *FirstDbgValue;
137 explicit ScheduleDAGInstrs(MachineFunction &mf,
138 const MachineLoopInfo &mli,
139 const MachineDominatorTree &mdt,
141 LiveIntervals *LIS = 0);
143 virtual ~ScheduleDAGInstrs() {}
145 /// \brief Expose LiveIntervals for use in DAG mutators and such.
146 LiveIntervals *getLIS() const { return LIS; }
148 /// \brief Get the machine model for instruction scheduling.
149 const TargetSchedModel *getSchedModel() const { return &SchedModel; }
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;
158 /// begin - Return an iterator to the top of the current scheduling region.
159 MachineBasicBlock::iterator begin() const { return RegionBegin; }
161 /// end - Return an iterator to the bottom of the current scheduling region.
162 MachineBasicBlock::iterator end() const { return RegionEnd; }
164 /// newSUnit - Creates a new SUnit and return a ptr to it.
165 SUnit *newSUnit(MachineInstr *MI);
167 /// getSUnit - Return an existing SUnit for this MI, or NULL.
168 SUnit *getSUnit(MachineInstr *MI) const;
170 /// startBlock - Prepare to perform scheduling in the given block.
171 virtual void startBlock(MachineBasicBlock *BB);
173 /// finishBlock - Clean up after scheduling in the given block.
174 virtual void finishBlock();
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);
182 /// Notify that the scheduler has finished scheduling the current region.
183 virtual void exitRegion();
185 /// buildSchedGraph - Build SUnits from the MachineBasicBlock that we are
187 void buildSchedGraph(AliasAnalysis *AA, RegPressureTracker *RPTracker = 0);
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();
198 /// schedule - Order nodes according to selected style, filling
199 /// in the Sequence member.
201 /// Typically, a scheduling algorithm will implement schedule() without
202 /// overriding enterRegion() or exitRegion().
203 virtual void schedule() = 0;
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() {}
209 virtual void dumpNode(const SUnit *SU) const;
211 /// Return a label for a DAG node that points to an instruction.
212 virtual std::string getGraphNodeLabel(const SUnit *SU) const;
214 /// Return a label for the region of code covered by the DAG.
215 virtual std::string getDAGName() const;
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);
225 /// newSUnit - Creates a new SUnit and return a ptr to it.
226 inline SUnit *ScheduleDAGInstrs::newSUnit(MachineInstr *MI) {
228 const SUnit *Addr = SUnits.empty() ? 0 : &SUnits[0];
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();
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())