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