2009-02-06 17:12:10 +00:00
|
|
|
//==- ScheduleDAGInstrs.h - MachineInstr Scheduling --------------*- C++ -*-==//
|
2008-11-19 23:18:57 +00:00
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This file implements the ScheduleDAGInstrs class, which implements
|
|
|
|
// scheduling for a MachineInstr-based dependency graph.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2013-01-10 00:45:19 +00:00
|
|
|
#ifndef LLVM_CODEGEN_SCHEDULEDAGINSTRS_H
|
|
|
|
#define LLVM_CODEGEN_SCHEDULEDAGINSTRS_H
|
2008-11-19 23:18:57 +00:00
|
|
|
|
2013-01-21 18:18:53 +00:00
|
|
|
#include "llvm/ADT/SparseMultiSet.h"
|
2014-01-07 11:48:04 +00:00
|
|
|
#include "llvm/ADT/SparseSet.h"
|
2008-11-19 23:18:57 +00:00
|
|
|
#include "llvm/CodeGen/ScheduleDAG.h"
|
2012-09-18 04:03:34 +00:00
|
|
|
#include "llvm/CodeGen/TargetSchedule.h"
|
2009-02-10 23:27:53 +00:00
|
|
|
#include "llvm/Support/Compiler.h"
|
2009-01-15 19:20:50 +00:00
|
|
|
#include "llvm/Target/TargetRegisterInfo.h"
|
2008-11-19 23:18:57 +00:00
|
|
|
|
|
|
|
namespace llvm {
|
2013-03-10 13:11:23 +00:00
|
|
|
class MachineFrameInfo;
|
2008-12-16 03:25:46 +00:00
|
|
|
class MachineLoopInfo;
|
|
|
|
class MachineDominatorTree;
|
2012-02-22 06:08:11 +00:00
|
|
|
class LiveIntervals;
|
2012-04-24 17:56:43 +00:00
|
|
|
class RegPressureTracker;
|
2013-08-30 03:49:48 +00:00
|
|
|
class PressureDiffs;
|
2008-12-16 03:25:46 +00:00
|
|
|
|
2012-03-07 23:00:57 +00:00
|
|
|
/// An individual mapping from virtual register number to SUnit.
|
|
|
|
struct VReg2SUnit {
|
|
|
|
unsigned VirtReg;
|
|
|
|
SUnit *SU;
|
|
|
|
|
|
|
|
VReg2SUnit(unsigned reg, SUnit *su): VirtReg(reg), SU(su) {}
|
|
|
|
|
2012-04-20 20:05:28 +00:00
|
|
|
unsigned getSparseSetIndex() const {
|
2012-03-07 23:00:57 +00:00
|
|
|
return TargetRegisterInfo::virtReg2Index(VirtReg);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2012-08-23 00:39:43 +00:00
|
|
|
/// Record a physical register access.
|
2013-12-05 05:44:44 +00:00
|
|
|
/// For non-data-dependent uses, OpIdx == -1.
|
2012-08-23 00:39:43 +00:00
|
|
|
struct PhysRegSUOper {
|
|
|
|
SUnit *SU;
|
|
|
|
int OpIdx;
|
2013-01-21 18:18:53 +00:00
|
|
|
unsigned Reg;
|
2012-08-23 00:39:43 +00:00
|
|
|
|
2013-01-21 18:18:53 +00:00
|
|
|
PhysRegSUOper(SUnit *su, int op, unsigned R): SU(su), OpIdx(op), Reg(R) {}
|
2012-03-07 23:00:57 +00:00
|
|
|
|
2013-01-21 18:18:53 +00:00
|
|
|
unsigned getSparseSetIndex() const { return Reg; }
|
2012-03-07 23:00:57 +00:00
|
|
|
};
|
|
|
|
|
2013-01-21 18:18:53 +00:00
|
|
|
/// Use a SparseMultiSet to track physical registers. Storage is only
|
|
|
|
/// allocated once for the pass. It can be cleared in constant time and reused
|
|
|
|
/// without any frees.
|
2013-08-23 17:48:39 +00:00
|
|
|
typedef SparseMultiSet<PhysRegSUOper, llvm::identity<unsigned>, uint16_t>
|
|
|
|
Reg2SUnitsMap;
|
2013-01-21 18:18:53 +00:00
|
|
|
|
2012-03-07 23:00:57 +00:00
|
|
|
/// Use SparseSet as a SparseMap by relying on the fact that it never
|
|
|
|
/// compares ValueT's, only unsigned keys. This allows the set to be cleared
|
|
|
|
/// between scheduling regions in constant time as long as ValueT does not
|
|
|
|
/// require a destructor.
|
2012-04-20 20:05:28 +00:00
|
|
|
typedef SparseSet<VReg2SUnit, VirtReg2IndexFunctor> VReg2SUnitMap;
|
2012-03-07 23:00:57 +00:00
|
|
|
|
2013-08-23 17:48:39 +00:00
|
|
|
/// Track local uses of virtual registers. These uses are gathered by the DAG
|
|
|
|
/// builder and may be consulted by the scheduler to avoid iterating an entire
|
|
|
|
/// vreg use list.
|
|
|
|
typedef SparseMultiSet<VReg2SUnit, VirtReg2IndexFunctor> VReg2UseMap;
|
|
|
|
|
2009-02-10 23:27:53 +00:00
|
|
|
/// ScheduleDAGInstrs - A ScheduleDAG subclass for scheduling lists of
|
|
|
|
/// MachineInstrs.
|
2012-03-07 23:01:02 +00:00
|
|
|
class ScheduleDAGInstrs : public ScheduleDAG {
|
2012-03-07 05:21:52 +00:00
|
|
|
protected:
|
2014-08-20 19:36:05 +00:00
|
|
|
const MachineLoopInfo *MLI;
|
2009-10-18 19:58:47 +00:00
|
|
|
const MachineFrameInfo *MFI;
|
2008-12-16 03:25:46 +00:00
|
|
|
|
2012-03-07 23:00:59 +00:00
|
|
|
/// Live Intervals provides reaching defs in preRA scheduling.
|
|
|
|
LiveIntervals *LIS;
|
|
|
|
|
2012-09-18 04:03:34 +00:00
|
|
|
/// TargetSchedModel provides an interface to the machine model.
|
|
|
|
TargetSchedModel SchedModel;
|
|
|
|
|
2012-01-14 02:17:12 +00:00
|
|
|
/// isPostRA flag indicates vregs cannot be present.
|
|
|
|
bool IsPostRA;
|
|
|
|
|
2013-12-28 21:56:55 +00:00
|
|
|
/// True if the DAG builder should remove kill flags (in preparation for
|
|
|
|
/// rescheduling).
|
|
|
|
bool RemoveKillFlags;
|
|
|
|
|
2012-04-13 23:29:54 +00:00
|
|
|
/// The standard DAG builder does not normally include terminators as DAG
|
|
|
|
/// nodes because it does not create the necessary dependencies to prevent
|
2014-01-24 17:20:08 +00:00
|
|
|
/// reordering. A specialized scheduler can override
|
2012-04-13 23:29:54 +00:00
|
|
|
/// TargetInstrInfo::isSchedulingBoundary then enable this flag to indicate
|
|
|
|
/// it has taken responsibility for scheduling the terminator correctly.
|
|
|
|
bool CanHandleTerminators;
|
|
|
|
|
2012-03-07 05:21:52 +00:00
|
|
|
/// State specific to the current scheduling region.
|
2012-03-07 23:00:59 +00:00
|
|
|
/// ------------------------------------------------
|
2012-03-07 05:21:52 +00:00
|
|
|
|
2012-03-07 23:00:59 +00:00
|
|
|
/// The block in which to insert instructions
|
2012-03-07 05:21:52 +00:00
|
|
|
MachineBasicBlock *BB;
|
|
|
|
|
2012-03-07 23:00:52 +00:00
|
|
|
/// The beginning of the range to be scheduled.
|
2012-03-09 04:29:02 +00:00
|
|
|
MachineBasicBlock::iterator RegionBegin;
|
2012-03-07 05:21:52 +00:00
|
|
|
|
2012-03-07 23:00:52 +00:00
|
|
|
/// The end of the range to be scheduled.
|
2012-03-09 04:29:02 +00:00
|
|
|
MachineBasicBlock::iterator RegionEnd;
|
2012-03-07 05:21:52 +00:00
|
|
|
|
2013-08-23 17:48:33 +00:00
|
|
|
/// Instructions in this region (distance(RegionBegin, RegionEnd)).
|
|
|
|
unsigned NumRegionInstrs;
|
2012-03-07 05:21:52 +00:00
|
|
|
|
2012-03-07 00:18:08 +00:00
|
|
|
/// After calling BuildSchedGraph, each machine instruction in the current
|
|
|
|
/// scheduling region is mapped to an SUnit.
|
2012-02-22 06:08:11 +00:00
|
|
|
DenseMap<MachineInstr*, SUnit*> MISUnitMap;
|
|
|
|
|
2013-08-23 17:48:39 +00:00
|
|
|
/// After calling BuildSchedGraph, each vreg used in the scheduling region
|
|
|
|
/// is mapped to a set of SUnits. These include all local vreg uses, not
|
|
|
|
/// just the uses for a singly defined vreg.
|
|
|
|
VReg2UseMap VRegUses;
|
|
|
|
|
2012-03-07 23:00:59 +00:00
|
|
|
/// State internal to DAG building.
|
|
|
|
/// -------------------------------
|
2012-01-14 02:17:15 +00:00
|
|
|
|
2012-02-24 07:04:55 +00:00
|
|
|
/// Defs, Uses - Remember where defs and uses of each register are as we
|
|
|
|
/// iterate upward through the instructions. This is allocated here instead
|
|
|
|
/// of inside BuildSchedGraph to avoid the need for it to be initialized and
|
|
|
|
/// destructed for each block.
|
|
|
|
Reg2SUnitsMap Defs;
|
|
|
|
Reg2SUnitsMap Uses;
|
2011-10-07 06:27:02 +00:00
|
|
|
|
2013-09-28 13:42:22 +00:00
|
|
|
/// Track the last instruction in this region defining each virtual register.
|
2012-02-22 21:59:00 +00:00
|
|
|
VReg2SUnitMap VRegDefs;
|
2012-01-14 02:17:18 +00:00
|
|
|
|
2009-01-15 19:20:50 +00:00
|
|
|
/// PendingLoads - Remember where unknown loads are after the most recent
|
|
|
|
/// unknown store, as we iterate. As with Defs and Uses, this is here
|
|
|
|
/// to minimize construction/destruction.
|
|
|
|
std::vector<SUnit *> PendingLoads;
|
|
|
|
|
2012-06-02 10:20:22 +00:00
|
|
|
/// DbgValues - Remember instruction that precedes DBG_VALUE.
|
2012-03-07 23:00:59 +00:00
|
|
|
/// These are generated by buildSchedGraph but persist so they can be
|
|
|
|
/// referenced when emitting the final schedule.
|
2011-10-07 06:27:02 +00:00
|
|
|
typedef std::vector<std::pair<MachineInstr *, MachineInstr *> >
|
2011-06-02 21:26:52 +00:00
|
|
|
DbgValueVector;
|
|
|
|
DbgValueVector DbgValues;
|
|
|
|
MachineInstr *FirstDbgValue;
|
|
|
|
|
2013-12-28 21:56:55 +00:00
|
|
|
/// Set of live physical registers for updating kill flags.
|
|
|
|
BitVector LiveRegs;
|
|
|
|
|
2008-11-19 23:18:57 +00:00
|
|
|
public:
|
2009-01-15 19:20:50 +00:00
|
|
|
explicit ScheduleDAGInstrs(MachineFunction &mf,
|
2014-08-20 19:36:05 +00:00
|
|
|
const MachineLoopInfo *mli,
|
2012-02-22 06:08:11 +00:00
|
|
|
bool IsPostRAFlag,
|
2013-12-28 21:56:55 +00:00
|
|
|
bool RemoveKillFlags = false,
|
2014-04-14 00:51:57 +00:00
|
|
|
LiveIntervals *LIS = nullptr);
|
2008-11-19 23:18:57 +00:00
|
|
|
|
|
|
|
virtual ~ScheduleDAGInstrs() {}
|
|
|
|
|
2013-12-28 21:56:55 +00:00
|
|
|
bool isPostRA() const { return IsPostRA; }
|
|
|
|
|
2013-04-24 15:54:43 +00:00
|
|
|
/// \brief Expose LiveIntervals for use in DAG mutators and such.
|
|
|
|
LiveIntervals *getLIS() const { return LIS; }
|
|
|
|
|
2012-10-10 05:43:09 +00:00
|
|
|
/// \brief Get the machine model for instruction scheduling.
|
|
|
|
const TargetSchedModel *getSchedModel() const { return &SchedModel; }
|
|
|
|
|
2012-11-06 07:10:38 +00:00
|
|
|
/// \brief Resolve and cache a resolved scheduling class for an SUnit.
|
|
|
|
const MCSchedClassDesc *getSchedClass(SUnit *SU) const {
|
2013-06-15 04:49:57 +00:00
|
|
|
if (!SU->SchedClass && SchedModel.hasInstrSchedModel())
|
2012-11-06 07:10:38 +00:00
|
|
|
SU->SchedClass = SchedModel.resolveSchedClass(SU->getInstr());
|
|
|
|
return SU->SchedClass;
|
|
|
|
}
|
|
|
|
|
2012-03-07 05:21:52 +00:00
|
|
|
/// begin - Return an iterator to the top of the current scheduling region.
|
2012-03-09 04:29:02 +00:00
|
|
|
MachineBasicBlock::iterator begin() const { return RegionBegin; }
|
2012-03-07 05:21:52 +00:00
|
|
|
|
|
|
|
/// end - Return an iterator to the bottom of the current scheduling region.
|
2012-03-09 04:29:02 +00:00
|
|
|
MachineBasicBlock::iterator end() const { return RegionEnd; }
|
2012-03-07 05:21:52 +00:00
|
|
|
|
2012-03-07 23:01:09 +00:00
|
|
|
/// newSUnit - Creates a new SUnit and return a ptr to it.
|
2012-03-07 23:00:57 +00:00
|
|
|
SUnit *newSUnit(MachineInstr *MI);
|
2008-11-19 23:18:57 +00:00
|
|
|
|
2012-03-07 23:01:09 +00:00
|
|
|
/// getSUnit - Return an existing SUnit for this MI, or NULL.
|
|
|
|
SUnit *getSUnit(MachineInstr *MI) const;
|
|
|
|
|
2012-03-07 23:00:49 +00:00
|
|
|
/// startBlock - Prepare to perform scheduling in the given block.
|
|
|
|
virtual void startBlock(MachineBasicBlock *BB);
|
2012-02-22 06:08:11 +00:00
|
|
|
|
2012-03-07 23:00:49 +00:00
|
|
|
/// finishBlock - Clean up after scheduling in the given block.
|
|
|
|
virtual void finishBlock();
|
2012-03-07 05:21:52 +00:00
|
|
|
|
|
|
|
/// Initialize the scheduler state for the next scheduling region.
|
|
|
|
virtual void enterRegion(MachineBasicBlock *bb,
|
|
|
|
MachineBasicBlock::iterator begin,
|
|
|
|
MachineBasicBlock::iterator end,
|
2013-08-23 17:48:33 +00:00
|
|
|
unsigned regioninstrs);
|
2012-03-07 05:21:52 +00:00
|
|
|
|
|
|
|
/// Notify that the scheduler has finished scheduling the current region.
|
|
|
|
virtual void exitRegion();
|
2009-02-11 04:27:20 +00:00
|
|
|
|
2012-03-07 23:00:49 +00:00
|
|
|
/// buildSchedGraph - Build SUnits from the MachineBasicBlock that we are
|
2008-11-19 23:18:57 +00:00
|
|
|
/// input.
|
2014-04-14 00:51:57 +00:00
|
|
|
void buildSchedGraph(AliasAnalysis *AA,
|
|
|
|
RegPressureTracker *RPTracker = nullptr,
|
|
|
|
PressureDiffs *PDiffs = nullptr);
|
2008-11-19 23:18:57 +00:00
|
|
|
|
2012-03-07 23:00:49 +00:00
|
|
|
/// addSchedBarrierDeps - Add dependencies from instructions in the current
|
2010-10-23 02:10:46 +00:00
|
|
|
/// list of instructions being scheduled to scheduling barrier. We want to
|
|
|
|
/// make sure instructions which define registers that are either used by
|
|
|
|
/// the terminator or are live-out are properly scheduled. This is
|
|
|
|
/// especially important when the definition latency of the return value(s)
|
|
|
|
/// are too high to be hidden by the branch or when the liveout registers
|
|
|
|
/// used by instructions in the fallthrough block.
|
2012-03-07 23:00:49 +00:00
|
|
|
void addSchedBarrierDeps();
|
2010-10-23 02:10:46 +00:00
|
|
|
|
2012-03-07 23:00:49 +00:00
|
|
|
/// schedule - Order nodes according to selected style, filling
|
2008-11-19 23:18:57 +00:00
|
|
|
/// in the Sequence member.
|
|
|
|
///
|
2012-03-07 23:00:49 +00:00
|
|
|
/// Typically, a scheduling algorithm will implement schedule() without
|
2012-03-07 05:21:54 +00:00
|
|
|
/// overriding enterRegion() or exitRegion().
|
2012-03-07 23:00:49 +00:00
|
|
|
virtual void schedule() = 0;
|
2008-11-19 23:18:57 +00:00
|
|
|
|
2012-04-01 07:24:23 +00:00
|
|
|
/// finalizeSchedule - Allow targets to perform final scheduling actions at
|
|
|
|
/// the level of the whole MachineFunction. By default does nothing.
|
|
|
|
virtual void finalizeSchedule() {}
|
|
|
|
|
2014-03-07 09:26:03 +00:00
|
|
|
void dumpNode(const SUnit *SU) const override;
|
2008-11-19 23:18:57 +00:00
|
|
|
|
2012-03-07 05:21:47 +00:00
|
|
|
/// Return a label for a DAG node that points to an instruction.
|
2014-03-07 09:26:03 +00:00
|
|
|
std::string getGraphNodeLabel(const SUnit *SU) const override;
|
2012-01-14 02:17:15 +00:00
|
|
|
|
2012-03-07 05:21:47 +00:00
|
|
|
/// Return a label for the region of code covered by the DAG.
|
2014-03-07 09:26:03 +00:00
|
|
|
std::string getDAGName() const override;
|
2012-03-07 00:18:22 +00:00
|
|
|
|
2013-12-28 21:56:55 +00:00
|
|
|
/// \brief Fix register kill flags that scheduling has made invalid.
|
|
|
|
void fixupKills(MachineBasicBlock *MBB);
|
2012-01-14 02:17:15 +00:00
|
|
|
protected:
|
2012-02-22 06:08:11 +00:00
|
|
|
void initSUnits();
|
2012-08-23 00:39:43 +00:00
|
|
|
void addPhysRegDataDeps(SUnit *SU, unsigned OperIdx);
|
2012-01-14 02:17:15 +00:00
|
|
|
void addPhysRegDeps(SUnit *SU, unsigned OperIdx);
|
2012-01-14 02:17:18 +00:00
|
|
|
void addVRegDefDeps(SUnit *SU, unsigned OperIdx);
|
|
|
|
void addVRegUseDeps(SUnit *SU, unsigned OperIdx);
|
2013-12-28 21:56:55 +00:00
|
|
|
|
|
|
|
/// \brief PostRA helper for rewriting kill flags.
|
|
|
|
void startBlockForKills(MachineBasicBlock *BB);
|
|
|
|
|
|
|
|
/// \brief Toggle a register operand kill flag.
|
|
|
|
///
|
|
|
|
/// Other adjustments may be made to the instruction if necessary. Return
|
|
|
|
/// true if the operand has been deleted, false if not.
|
|
|
|
bool toggleKillFlag(MachineInstr *MI, MachineOperand &MO);
|
2008-11-19 23:18:57 +00:00
|
|
|
};
|
2012-03-07 23:00:57 +00:00
|
|
|
|
2012-03-07 23:01:09 +00:00
|
|
|
/// newSUnit - Creates a new SUnit and return a ptr to it.
|
2012-03-07 23:00:57 +00:00
|
|
|
inline SUnit *ScheduleDAGInstrs::newSUnit(MachineInstr *MI) {
|
|
|
|
#ifndef NDEBUG
|
2014-04-14 00:51:57 +00:00
|
|
|
const SUnit *Addr = SUnits.empty() ? nullptr : &SUnits[0];
|
2012-03-07 23:00:57 +00:00
|
|
|
#endif
|
|
|
|
SUnits.push_back(SUnit(MI, (unsigned)SUnits.size()));
|
2014-04-14 00:51:57 +00:00
|
|
|
assert((Addr == nullptr || Addr == &SUnits[0]) &&
|
2012-03-07 23:00:57 +00:00
|
|
|
"SUnits std::vector reallocated on the fly!");
|
|
|
|
SUnits.back().OrigNode = &SUnits.back();
|
|
|
|
return &SUnits.back();
|
|
|
|
}
|
2012-03-07 23:01:09 +00:00
|
|
|
|
|
|
|
/// getSUnit - Return an existing SUnit for this MI, or NULL.
|
|
|
|
inline SUnit *ScheduleDAGInstrs::getSUnit(MachineInstr *MI) const {
|
|
|
|
DenseMap<MachineInstr*, SUnit*>::const_iterator I = MISUnitMap.find(MI);
|
|
|
|
if (I == MISUnitMap.end())
|
2014-04-14 00:51:57 +00:00
|
|
|
return nullptr;
|
2012-03-07 23:01:09 +00:00
|
|
|
return I->second;
|
|
|
|
}
|
2012-03-07 23:00:57 +00:00
|
|
|
} // namespace llvm
|
2008-11-19 23:18:57 +00:00
|
|
|
|
|
|
|
#endif
|