LLVM API Documentation
00001 //==- ScheduleDAGInstrs.h - MachineInstr Scheduling --------------*- C++ -*-==// 00002 // 00003 // The LLVM Compiler Infrastructure 00004 // 00005 // This file is distributed under the University of Illinois Open Source 00006 // License. See LICENSE.TXT for details. 00007 // 00008 //===----------------------------------------------------------------------===// 00009 // 00010 // This file implements the ScheduleDAGInstrs class, which implements 00011 // scheduling for a MachineInstr-based dependency graph. 00012 // 00013 //===----------------------------------------------------------------------===// 00014 00015 #ifndef LLVM_CODEGEN_SCHEDULEDAGINSTRS_H 00016 #define LLVM_CODEGEN_SCHEDULEDAGINSTRS_H 00017 00018 #include "llvm/ADT/SparseMultiSet.h" 00019 #include "llvm/ADT/SparseSet.h" 00020 #include "llvm/CodeGen/ScheduleDAG.h" 00021 #include "llvm/CodeGen/TargetSchedule.h" 00022 #include "llvm/Support/Compiler.h" 00023 #include "llvm/Target/TargetRegisterInfo.h" 00024 00025 namespace llvm { 00026 class MachineFrameInfo; 00027 class MachineLoopInfo; 00028 class MachineDominatorTree; 00029 class LiveIntervals; 00030 class RegPressureTracker; 00031 class PressureDiffs; 00032 00033 /// An individual mapping from virtual register number to SUnit. 00034 struct VReg2SUnit { 00035 unsigned VirtReg; 00036 SUnit *SU; 00037 00038 VReg2SUnit(unsigned reg, SUnit *su): VirtReg(reg), SU(su) {} 00039 00040 unsigned getSparseSetIndex() const { 00041 return TargetRegisterInfo::virtReg2Index(VirtReg); 00042 } 00043 }; 00044 00045 /// Record a physical register access. 00046 /// For non-data-dependent uses, OpIdx == -1. 00047 struct PhysRegSUOper { 00048 SUnit *SU; 00049 int OpIdx; 00050 unsigned Reg; 00051 00052 PhysRegSUOper(SUnit *su, int op, unsigned R): SU(su), OpIdx(op), Reg(R) {} 00053 00054 unsigned getSparseSetIndex() const { return Reg; } 00055 }; 00056 00057 /// Use a SparseMultiSet to track physical registers. Storage is only 00058 /// allocated once for the pass. It can be cleared in constant time and reused 00059 /// without any frees. 00060 typedef SparseMultiSet<PhysRegSUOper, llvm::identity<unsigned>, uint16_t> 00061 Reg2SUnitsMap; 00062 00063 /// Use SparseSet as a SparseMap by relying on the fact that it never 00064 /// compares ValueT's, only unsigned keys. This allows the set to be cleared 00065 /// between scheduling regions in constant time as long as ValueT does not 00066 /// require a destructor. 00067 typedef SparseSet<VReg2SUnit, VirtReg2IndexFunctor> VReg2SUnitMap; 00068 00069 /// Track local uses of virtual registers. These uses are gathered by the DAG 00070 /// builder and may be consulted by the scheduler to avoid iterating an entire 00071 /// vreg use list. 00072 typedef SparseMultiSet<VReg2SUnit, VirtReg2IndexFunctor> VReg2UseMap; 00073 00074 /// ScheduleDAGInstrs - A ScheduleDAG subclass for scheduling lists of 00075 /// MachineInstrs. 00076 class ScheduleDAGInstrs : public ScheduleDAG { 00077 protected: 00078 const MachineLoopInfo *MLI; 00079 const MachineFrameInfo *MFI; 00080 00081 /// Live Intervals provides reaching defs in preRA scheduling. 00082 LiveIntervals *LIS; 00083 00084 /// TargetSchedModel provides an interface to the machine model. 00085 TargetSchedModel SchedModel; 00086 00087 /// isPostRA flag indicates vregs cannot be present. 00088 bool IsPostRA; 00089 00090 /// True if the DAG builder should remove kill flags (in preparation for 00091 /// rescheduling). 00092 bool RemoveKillFlags; 00093 00094 /// The standard DAG builder does not normally include terminators as DAG 00095 /// nodes because it does not create the necessary dependencies to prevent 00096 /// reordering. A specialized scheduler can override 00097 /// TargetInstrInfo::isSchedulingBoundary then enable this flag to indicate 00098 /// it has taken responsibility for scheduling the terminator correctly. 00099 bool CanHandleTerminators; 00100 00101 /// State specific to the current scheduling region. 00102 /// ------------------------------------------------ 00103 00104 /// The block in which to insert instructions 00105 MachineBasicBlock *BB; 00106 00107 /// The beginning of the range to be scheduled. 00108 MachineBasicBlock::iterator RegionBegin; 00109 00110 /// The end of the range to be scheduled. 00111 MachineBasicBlock::iterator RegionEnd; 00112 00113 /// Instructions in this region (distance(RegionBegin, RegionEnd)). 00114 unsigned NumRegionInstrs; 00115 00116 /// After calling BuildSchedGraph, each machine instruction in the current 00117 /// scheduling region is mapped to an SUnit. 00118 DenseMap<MachineInstr*, SUnit*> MISUnitMap; 00119 00120 /// After calling BuildSchedGraph, each vreg used in the scheduling region 00121 /// is mapped to a set of SUnits. These include all local vreg uses, not 00122 /// just the uses for a singly defined vreg. 00123 VReg2UseMap VRegUses; 00124 00125 /// State internal to DAG building. 00126 /// ------------------------------- 00127 00128 /// Defs, Uses - Remember where defs and uses of each register are as we 00129 /// iterate upward through the instructions. This is allocated here instead 00130 /// of inside BuildSchedGraph to avoid the need for it to be initialized and 00131 /// destructed for each block. 00132 Reg2SUnitsMap Defs; 00133 Reg2SUnitsMap Uses; 00134 00135 /// Track the last instruction in this region defining each virtual register. 00136 VReg2SUnitMap VRegDefs; 00137 00138 /// PendingLoads - Remember where unknown loads are after the most recent 00139 /// unknown store, as we iterate. As with Defs and Uses, this is here 00140 /// to minimize construction/destruction. 00141 std::vector<SUnit *> PendingLoads; 00142 00143 /// DbgValues - Remember instruction that precedes DBG_VALUE. 00144 /// These are generated by buildSchedGraph but persist so they can be 00145 /// referenced when emitting the final schedule. 00146 typedef std::vector<std::pair<MachineInstr *, MachineInstr *> > 00147 DbgValueVector; 00148 DbgValueVector DbgValues; 00149 MachineInstr *FirstDbgValue; 00150 00151 /// Set of live physical registers for updating kill flags. 00152 BitVector LiveRegs; 00153 00154 public: 00155 explicit ScheduleDAGInstrs(MachineFunction &mf, 00156 const MachineLoopInfo *mli, 00157 bool IsPostRAFlag, 00158 bool RemoveKillFlags = false, 00159 LiveIntervals *LIS = nullptr); 00160 00161 virtual ~ScheduleDAGInstrs() {} 00162 00163 bool isPostRA() const { return IsPostRA; } 00164 00165 /// \brief Expose LiveIntervals for use in DAG mutators and such. 00166 LiveIntervals *getLIS() const { return LIS; } 00167 00168 /// \brief Get the machine model for instruction scheduling. 00169 const TargetSchedModel *getSchedModel() const { return &SchedModel; } 00170 00171 /// \brief Resolve and cache a resolved scheduling class for an SUnit. 00172 const MCSchedClassDesc *getSchedClass(SUnit *SU) const { 00173 if (!SU->SchedClass && SchedModel.hasInstrSchedModel()) 00174 SU->SchedClass = SchedModel.resolveSchedClass(SU->getInstr()); 00175 return SU->SchedClass; 00176 } 00177 00178 /// begin - Return an iterator to the top of the current scheduling region. 00179 MachineBasicBlock::iterator begin() const { return RegionBegin; } 00180 00181 /// end - Return an iterator to the bottom of the current scheduling region. 00182 MachineBasicBlock::iterator end() const { return RegionEnd; } 00183 00184 /// newSUnit - Creates a new SUnit and return a ptr to it. 00185 SUnit *newSUnit(MachineInstr *MI); 00186 00187 /// getSUnit - Return an existing SUnit for this MI, or NULL. 00188 SUnit *getSUnit(MachineInstr *MI) const; 00189 00190 /// startBlock - Prepare to perform scheduling in the given block. 00191 virtual void startBlock(MachineBasicBlock *BB); 00192 00193 /// finishBlock - Clean up after scheduling in the given block. 00194 virtual void finishBlock(); 00195 00196 /// Initialize the scheduler state for the next scheduling region. 00197 virtual void enterRegion(MachineBasicBlock *bb, 00198 MachineBasicBlock::iterator begin, 00199 MachineBasicBlock::iterator end, 00200 unsigned regioninstrs); 00201 00202 /// Notify that the scheduler has finished scheduling the current region. 00203 virtual void exitRegion(); 00204 00205 /// buildSchedGraph - Build SUnits from the MachineBasicBlock that we are 00206 /// input. 00207 void buildSchedGraph(AliasAnalysis *AA, 00208 RegPressureTracker *RPTracker = nullptr, 00209 PressureDiffs *PDiffs = nullptr); 00210 00211 /// addSchedBarrierDeps - Add dependencies from instructions in the current 00212 /// list of instructions being scheduled to scheduling barrier. We want to 00213 /// make sure instructions which define registers that are either used by 00214 /// the terminator or are live-out are properly scheduled. This is 00215 /// especially important when the definition latency of the return value(s) 00216 /// are too high to be hidden by the branch or when the liveout registers 00217 /// used by instructions in the fallthrough block. 00218 void addSchedBarrierDeps(); 00219 00220 /// schedule - Order nodes according to selected style, filling 00221 /// in the Sequence member. 00222 /// 00223 /// Typically, a scheduling algorithm will implement schedule() without 00224 /// overriding enterRegion() or exitRegion(). 00225 virtual void schedule() = 0; 00226 00227 /// finalizeSchedule - Allow targets to perform final scheduling actions at 00228 /// the level of the whole MachineFunction. By default does nothing. 00229 virtual void finalizeSchedule() {} 00230 00231 void dumpNode(const SUnit *SU) const override; 00232 00233 /// Return a label for a DAG node that points to an instruction. 00234 std::string getGraphNodeLabel(const SUnit *SU) const override; 00235 00236 /// Return a label for the region of code covered by the DAG. 00237 std::string getDAGName() const override; 00238 00239 /// \brief Fix register kill flags that scheduling has made invalid. 00240 void fixupKills(MachineBasicBlock *MBB); 00241 protected: 00242 void initSUnits(); 00243 void addPhysRegDataDeps(SUnit *SU, unsigned OperIdx); 00244 void addPhysRegDeps(SUnit *SU, unsigned OperIdx); 00245 void addVRegDefDeps(SUnit *SU, unsigned OperIdx); 00246 void addVRegUseDeps(SUnit *SU, unsigned OperIdx); 00247 00248 /// \brief PostRA helper for rewriting kill flags. 00249 void startBlockForKills(MachineBasicBlock *BB); 00250 00251 /// \brief Toggle a register operand kill flag. 00252 /// 00253 /// Other adjustments may be made to the instruction if necessary. Return 00254 /// true if the operand has been deleted, false if not. 00255 bool toggleKillFlag(MachineInstr *MI, MachineOperand &MO); 00256 }; 00257 00258 /// newSUnit - Creates a new SUnit and return a ptr to it. 00259 inline SUnit *ScheduleDAGInstrs::newSUnit(MachineInstr *MI) { 00260 #ifndef NDEBUG 00261 const SUnit *Addr = SUnits.empty() ? nullptr : &SUnits[0]; 00262 #endif 00263 SUnits.push_back(SUnit(MI, (unsigned)SUnits.size())); 00264 assert((Addr == nullptr || Addr == &SUnits[0]) && 00265 "SUnits std::vector reallocated on the fly!"); 00266 SUnits.back().OrigNode = &SUnits.back(); 00267 return &SUnits.back(); 00268 } 00269 00270 /// getSUnit - Return an existing SUnit for this MI, or NULL. 00271 inline SUnit *ScheduleDAGInstrs::getSUnit(MachineInstr *MI) const { 00272 DenseMap<MachineInstr*, SUnit*>::const_iterator I = MISUnitMap.find(MI); 00273 if (I == MISUnitMap.end()) 00274 return nullptr; 00275 return I->second; 00276 } 00277 } // namespace llvm 00278 00279 #endif