LLVM API Documentation

ScheduleDAGInstrs.h
Go to the documentation of this file.
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/SparseSet.h"
00019 #include "llvm/ADT/SparseMultiSet.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 
00032   /// An individual mapping from virtual register number to SUnit.
00033   struct VReg2SUnit {
00034     unsigned VirtReg;
00035     SUnit *SU;
00036 
00037     VReg2SUnit(unsigned reg, SUnit *su): VirtReg(reg), SU(su) {}
00038 
00039     unsigned getSparseSetIndex() const {
00040       return TargetRegisterInfo::virtReg2Index(VirtReg);
00041     }
00042   };
00043 
00044   /// Record a physical register access.
00045   /// For non data-dependent uses, OpIdx == -1.
00046   struct PhysRegSUOper {
00047     SUnit *SU;
00048     int OpIdx;
00049     unsigned Reg;
00050 
00051     PhysRegSUOper(SUnit *su, int op, unsigned R): SU(su), OpIdx(op), Reg(R) {}
00052 
00053     unsigned getSparseSetIndex() const { return Reg; }
00054   };
00055 
00056   /// Use a SparseMultiSet to track physical registers. Storage is only
00057   /// allocated once for the pass. It can be cleared in constant time and reused
00058   /// without any frees.
00059   typedef SparseMultiSet<PhysRegSUOper, llvm::identity<unsigned>, uint16_t> Reg2SUnitsMap;
00060 
00061   /// Use SparseSet as a SparseMap by relying on the fact that it never
00062   /// compares ValueT's, only unsigned keys. This allows the set to be cleared
00063   /// between scheduling regions in constant time as long as ValueT does not
00064   /// require a destructor.
00065   typedef SparseSet<VReg2SUnit, VirtReg2IndexFunctor> VReg2SUnitMap;
00066 
00067   /// ScheduleDAGInstrs - A ScheduleDAG subclass for scheduling lists of
00068   /// MachineInstrs.
00069   class ScheduleDAGInstrs : public ScheduleDAG {
00070   protected:
00071     const MachineLoopInfo &MLI;
00072     const MachineDominatorTree &MDT;
00073     const MachineFrameInfo *MFI;
00074 
00075     /// Live Intervals provides reaching defs in preRA scheduling.
00076     LiveIntervals *LIS;
00077 
00078     /// TargetSchedModel provides an interface to the machine model.
00079     TargetSchedModel SchedModel;
00080 
00081     /// isPostRA flag indicates vregs cannot be present.
00082     bool IsPostRA;
00083 
00084     /// UnitLatencies (misnamed) flag avoids computing def-use latencies, using
00085     /// the def-side latency only.
00086     bool UnitLatencies;
00087 
00088     /// The standard DAG builder does not normally include terminators as DAG
00089     /// nodes because it does not create the necessary dependencies to prevent
00090     /// reordering. A specialized scheduler can overide
00091     /// TargetInstrInfo::isSchedulingBoundary then enable this flag to indicate
00092     /// it has taken responsibility for scheduling the terminator correctly.
00093     bool CanHandleTerminators;
00094 
00095     /// State specific to the current scheduling region.
00096     /// ------------------------------------------------
00097 
00098     /// The block in which to insert instructions
00099     MachineBasicBlock *BB;
00100 
00101     /// The beginning of the range to be scheduled.
00102     MachineBasicBlock::iterator RegionBegin;
00103 
00104     /// The end of the range to be scheduled.
00105     MachineBasicBlock::iterator RegionEnd;
00106 
00107     /// The index in BB of RegionEnd.
00108     ///
00109     /// This is the instruction number from the top of the current block, not
00110     /// the SlotIndex. It is only used by the AntiDepBreaker and should be
00111     /// removed once that client is obsolete.
00112     unsigned EndIndex;
00113 
00114     /// After calling BuildSchedGraph, each machine instruction in the current
00115     /// scheduling region is mapped to an SUnit.
00116     DenseMap<MachineInstr*, SUnit*> MISUnitMap;
00117 
00118     /// State internal to DAG building.
00119     /// -------------------------------
00120 
00121     /// Defs, Uses - Remember where defs and uses of each register are as we
00122     /// iterate upward through the instructions. This is allocated here instead
00123     /// of inside BuildSchedGraph to avoid the need for it to be initialized and
00124     /// destructed for each block.
00125     Reg2SUnitsMap Defs;
00126     Reg2SUnitsMap Uses;
00127 
00128     /// Track the last instructon in this region defining each virtual register.
00129     VReg2SUnitMap VRegDefs;
00130 
00131     /// PendingLoads - Remember where unknown loads are after the most recent
00132     /// unknown store, as we iterate. As with Defs and Uses, this is here
00133     /// to minimize construction/destruction.
00134     std::vector<SUnit *> PendingLoads;
00135 
00136     /// DbgValues - Remember instruction that precedes DBG_VALUE.
00137     /// These are generated by buildSchedGraph but persist so they can be
00138     /// referenced when emitting the final schedule.
00139     typedef std::vector<std::pair<MachineInstr *, MachineInstr *> >
00140       DbgValueVector;
00141     DbgValueVector DbgValues;
00142     MachineInstr *FirstDbgValue;
00143 
00144   public:
00145     explicit ScheduleDAGInstrs(MachineFunction &mf,
00146                                const MachineLoopInfo &mli,
00147                                const MachineDominatorTree &mdt,
00148                                bool IsPostRAFlag,
00149                                LiveIntervals *LIS = 0);
00150 
00151     virtual ~ScheduleDAGInstrs() {}
00152 
00153     /// \brief Expose LiveIntervals for use in DAG mutators and such.
00154     LiveIntervals *getLIS() const { return LIS; }
00155 
00156     /// \brief Get the machine model for instruction scheduling.
00157     const TargetSchedModel *getSchedModel() const { return &SchedModel; }
00158 
00159     /// \brief Resolve and cache a resolved scheduling class for an SUnit.
00160     const MCSchedClassDesc *getSchedClass(SUnit *SU) const {
00161       if (!SU->SchedClass)
00162         SU->SchedClass = SchedModel.resolveSchedClass(SU->getInstr());
00163       return SU->SchedClass;
00164     }
00165 
00166     /// begin - Return an iterator to the top of the current scheduling region.
00167     MachineBasicBlock::iterator begin() const { return RegionBegin; }
00168 
00169     /// end - Return an iterator to the bottom of the current scheduling region.
00170     MachineBasicBlock::iterator end() const { return RegionEnd; }
00171 
00172     /// newSUnit - Creates a new SUnit and return a ptr to it.
00173     SUnit *newSUnit(MachineInstr *MI);
00174 
00175     /// getSUnit - Return an existing SUnit for this MI, or NULL.
00176     SUnit *getSUnit(MachineInstr *MI) const;
00177 
00178     /// startBlock - Prepare to perform scheduling in the given block.
00179     virtual void startBlock(MachineBasicBlock *BB);
00180 
00181     /// finishBlock - Clean up after scheduling in the given block.
00182     virtual void finishBlock();
00183 
00184     /// Initialize the scheduler state for the next scheduling region.
00185     virtual void enterRegion(MachineBasicBlock *bb,
00186                              MachineBasicBlock::iterator begin,
00187                              MachineBasicBlock::iterator end,
00188                              unsigned endcount);
00189 
00190     /// Notify that the scheduler has finished scheduling the current region.
00191     virtual void exitRegion();
00192 
00193     /// buildSchedGraph - Build SUnits from the MachineBasicBlock that we are
00194     /// input.
00195     void buildSchedGraph(AliasAnalysis *AA, RegPressureTracker *RPTracker = 0);
00196 
00197     /// addSchedBarrierDeps - Add dependencies from instructions in the current
00198     /// list of instructions being scheduled to scheduling barrier. We want to
00199     /// make sure instructions which define registers that are either used by
00200     /// the terminator or are live-out are properly scheduled. This is
00201     /// especially important when the definition latency of the return value(s)
00202     /// are too high to be hidden by the branch or when the liveout registers
00203     /// used by instructions in the fallthrough block.
00204     void addSchedBarrierDeps();
00205 
00206     /// schedule - Order nodes according to selected style, filling
00207     /// in the Sequence member.
00208     ///
00209     /// Typically, a scheduling algorithm will implement schedule() without
00210     /// overriding enterRegion() or exitRegion().
00211     virtual void schedule() = 0;
00212 
00213     /// finalizeSchedule - Allow targets to perform final scheduling actions at
00214     /// the level of the whole MachineFunction. By default does nothing.
00215     virtual void finalizeSchedule() {}
00216 
00217     virtual void dumpNode(const SUnit *SU) const;
00218 
00219     /// Return a label for a DAG node that points to an instruction.
00220     virtual std::string getGraphNodeLabel(const SUnit *SU) const;
00221 
00222     /// Return a label for the region of code covered by the DAG.
00223     virtual std::string getDAGName() const;
00224 
00225   protected:
00226     void initSUnits();
00227     void addPhysRegDataDeps(SUnit *SU, unsigned OperIdx);
00228     void addPhysRegDeps(SUnit *SU, unsigned OperIdx);
00229     void addVRegDefDeps(SUnit *SU, unsigned OperIdx);
00230     void addVRegUseDeps(SUnit *SU, unsigned OperIdx);
00231   };
00232 
00233   /// newSUnit - Creates a new SUnit and return a ptr to it.
00234   inline SUnit *ScheduleDAGInstrs::newSUnit(MachineInstr *MI) {
00235 #ifndef NDEBUG
00236     const SUnit *Addr = SUnits.empty() ? 0 : &SUnits[0];
00237 #endif
00238     SUnits.push_back(SUnit(MI, (unsigned)SUnits.size()));
00239     assert((Addr == 0 || Addr == &SUnits[0]) &&
00240            "SUnits std::vector reallocated on the fly!");
00241     SUnits.back().OrigNode = &SUnits.back();
00242     return &SUnits.back();
00243   }
00244 
00245   /// getSUnit - Return an existing SUnit for this MI, or NULL.
00246   inline SUnit *ScheduleDAGInstrs::getSUnit(MachineInstr *MI) const {
00247     DenseMap<MachineInstr*, SUnit*>::const_iterator I = MISUnitMap.find(MI);
00248     if (I == MISUnitMap.end())
00249       return 0;
00250     return I->second;
00251   }
00252 } // namespace llvm
00253 
00254 #endif