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/SparseMultiSet.h"
19 #include "llvm/ADT/SparseSet.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;
33 /// An individual mapping from virtual register number to SUnit.
38 VReg2SUnit(unsigned reg, SUnit *su): VirtReg(reg), SU(su) {}
40 unsigned getSparseSetIndex() const {
41 return TargetRegisterInfo::virtReg2Index(VirtReg);
45 /// Record a physical register access.
46 /// For non-data-dependent uses, OpIdx == -1.
47 struct PhysRegSUOper {
52 PhysRegSUOper(SUnit *su, int op, unsigned R): SU(su), OpIdx(op), Reg(R) {}
54 unsigned getSparseSetIndex() const { return Reg; }
57 /// Use a SparseMultiSet to track physical registers. Storage is only
58 /// allocated once for the pass. It can be cleared in constant time and reused
59 /// without any frees.
60 typedef SparseMultiSet<PhysRegSUOper, llvm::identity<unsigned>, uint16_t>
63 /// Use SparseSet as a SparseMap by relying on the fact that it never
64 /// compares ValueT's, only unsigned keys. This allows the set to be cleared
65 /// between scheduling regions in constant time as long as ValueT does not
66 /// require a destructor.
67 typedef SparseSet<VReg2SUnit, VirtReg2IndexFunctor> VReg2SUnitMap;
69 /// Track local uses of virtual registers. These uses are gathered by the DAG
70 /// builder and may be consulted by the scheduler to avoid iterating an entire
72 typedef SparseMultiSet<VReg2SUnit, VirtReg2IndexFunctor> VReg2UseMap;
74 /// ScheduleDAGInstrs - A ScheduleDAG subclass for scheduling lists of
76 class ScheduleDAGInstrs : public ScheduleDAG {
78 const MachineLoopInfo *MLI;
79 const MachineFrameInfo *MFI;
81 /// Live Intervals provides reaching defs in preRA scheduling.
84 /// TargetSchedModel provides an interface to the machine model.
85 TargetSchedModel SchedModel;
87 /// True if the DAG builder should remove kill flags (in preparation for
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 override
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 /// Instructions in this region (distance(RegionBegin, RegionEnd)).
111 unsigned NumRegionInstrs;
113 /// After calling BuildSchedGraph, each machine instruction in the current
114 /// scheduling region is mapped to an SUnit.
115 DenseMap<MachineInstr*, SUnit*> MISUnitMap;
117 /// After calling BuildSchedGraph, each vreg used in the scheduling region
118 /// is mapped to a set of SUnits. These include all local vreg uses, not
119 /// just the uses for a singly defined vreg.
120 VReg2UseMap VRegUses;
122 /// State internal to DAG building.
123 /// -------------------------------
125 /// Defs, Uses - Remember where defs and uses of each register are as we
126 /// iterate upward through the instructions. This is allocated here instead
127 /// of inside BuildSchedGraph to avoid the need for it to be initialized and
128 /// destructed for each block.
132 /// Track the last instruction in this region defining each virtual register.
133 VReg2SUnitMap VRegDefs;
135 /// PendingLoads - Remember where unknown loads are after the most recent
136 /// unknown store, as we iterate. As with Defs and Uses, this is here
137 /// to minimize construction/destruction.
138 std::vector<SUnit *> PendingLoads;
140 /// DbgValues - Remember instruction that precedes DBG_VALUE.
141 /// These are generated by buildSchedGraph but persist so they can be
142 /// referenced when emitting the final schedule.
143 typedef std::vector<std::pair<MachineInstr *, MachineInstr *> >
145 DbgValueVector DbgValues;
146 MachineInstr *FirstDbgValue;
148 /// Set of live physical registers for updating kill flags.
152 explicit ScheduleDAGInstrs(MachineFunction &mf,
153 const MachineLoopInfo *mli,
154 LiveIntervals *LIS = nullptr,
155 bool RemoveKillFlags = false);
157 ~ScheduleDAGInstrs() override {}
159 /// \brief Expose LiveIntervals for use in DAG mutators and such.
160 LiveIntervals *getLIS() const { return LIS; }
162 /// \brief Get the machine model for instruction scheduling.
163 const TargetSchedModel *getSchedModel() const { return &SchedModel; }
165 /// \brief Resolve and cache a resolved scheduling class for an SUnit.
166 const MCSchedClassDesc *getSchedClass(SUnit *SU) const {
167 if (!SU->SchedClass && SchedModel.hasInstrSchedModel())
168 SU->SchedClass = SchedModel.resolveSchedClass(SU->getInstr());
169 return SU->SchedClass;
172 /// begin - Return an iterator to the top of the current scheduling region.
173 MachineBasicBlock::iterator begin() const { return RegionBegin; }
175 /// end - Return an iterator to the bottom of the current scheduling region.
176 MachineBasicBlock::iterator end() const { return RegionEnd; }
178 /// newSUnit - Creates a new SUnit and return a ptr to it.
179 SUnit *newSUnit(MachineInstr *MI);
181 /// getSUnit - Return an existing SUnit for this MI, or NULL.
182 SUnit *getSUnit(MachineInstr *MI) const;
184 /// startBlock - Prepare to perform scheduling in the given block.
185 virtual void startBlock(MachineBasicBlock *BB);
187 /// finishBlock - Clean up after scheduling in the given block.
188 virtual void finishBlock();
190 /// Initialize the scheduler state for the next scheduling region.
191 virtual void enterRegion(MachineBasicBlock *bb,
192 MachineBasicBlock::iterator begin,
193 MachineBasicBlock::iterator end,
194 unsigned regioninstrs);
196 /// Notify that the scheduler has finished scheduling the current region.
197 virtual void exitRegion();
199 /// buildSchedGraph - Build SUnits from the MachineBasicBlock that we are
201 void buildSchedGraph(AliasAnalysis *AA,
202 RegPressureTracker *RPTracker = nullptr,
203 PressureDiffs *PDiffs = nullptr);
205 /// addSchedBarrierDeps - Add dependencies from instructions in the current
206 /// list of instructions being scheduled to scheduling barrier. We want to
207 /// make sure instructions which define registers that are either used by
208 /// the terminator or are live-out are properly scheduled. This is
209 /// especially important when the definition latency of the return value(s)
210 /// are too high to be hidden by the branch or when the liveout registers
211 /// used by instructions in the fallthrough block.
212 void addSchedBarrierDeps();
214 /// schedule - Order nodes according to selected style, filling
215 /// in the Sequence member.
217 /// Typically, a scheduling algorithm will implement schedule() without
218 /// overriding enterRegion() or exitRegion().
219 virtual void schedule() = 0;
221 /// finalizeSchedule - Allow targets to perform final scheduling actions at
222 /// the level of the whole MachineFunction. By default does nothing.
223 virtual void finalizeSchedule() {}
225 void dumpNode(const SUnit *SU) const override;
227 /// Return a label for a DAG node that points to an instruction.
228 std::string getGraphNodeLabel(const SUnit *SU) const override;
230 /// Return a label for the region of code covered by the DAG.
231 std::string getDAGName() const override;
233 /// \brief Fix register kill flags that scheduling has made invalid.
234 void fixupKills(MachineBasicBlock *MBB);
237 void addPhysRegDataDeps(SUnit *SU, unsigned OperIdx);
238 void addPhysRegDeps(SUnit *SU, unsigned OperIdx);
239 void addVRegDefDeps(SUnit *SU, unsigned OperIdx);
240 void addVRegUseDeps(SUnit *SU, unsigned OperIdx);
242 /// \brief PostRA helper for rewriting kill flags.
243 void startBlockForKills(MachineBasicBlock *BB);
245 /// \brief Toggle a register operand kill flag.
247 /// Other adjustments may be made to the instruction if necessary. Return
248 /// true if the operand has been deleted, false if not.
249 bool toggleKillFlag(MachineInstr *MI, MachineOperand &MO);
252 /// newSUnit - Creates a new SUnit and return a ptr to it.
253 inline SUnit *ScheduleDAGInstrs::newSUnit(MachineInstr *MI) {
255 const SUnit *Addr = SUnits.empty() ? nullptr : &SUnits[0];
257 SUnits.emplace_back(MI, (unsigned)SUnits.size());
258 assert((Addr == nullptr || Addr == &SUnits[0]) &&
259 "SUnits std::vector reallocated on the fly!");
260 SUnits.back().OrigNode = &SUnits.back();
261 return &SUnits.back();
264 /// getSUnit - Return an existing SUnit for this MI, or NULL.
265 inline SUnit *ScheduleDAGInstrs::getSUnit(MachineInstr *MI) const {
266 DenseMap<MachineInstr*, SUnit*>::const_iterator I = MISUnitMap.find(MI);
267 if (I == MISUnitMap.end())