22 #include "llvm/Config/llvm-config.h" 31 #include <type_traits> 36 #define DEBUG_TYPE "machine-scheduler" 58 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP) 66 auto BB = Begin->getParent();
68 << BB->getName() <<
":\n";
70 MaxInstNum =
std::max(MaxInstNum, 1u);
71 for (;
I != End && MaxInstNum; ++
I, --MaxInstNum) {
72 if (!
I->isDebugInstr() && LIS)
79 if (!
I->isDebugInstr() && LIS)
83 if (End != BB->end()) {
95 const auto BB = Begin->getParent();
96 const auto &
MRI = BB->getParent()->getRegInfo();
102 const auto BottomMI = End == BB->end() ? std::prev(End) : End;
112 OS <<
"Region to schedule ";
116 R->MaxPressure.print(OS, &
ST);
124 OS <<
"\nAfter scheduling ";
151 auto BB = R.
Begin->getParent();
152 Sch.BaseClass::startBlock(
BB);
162 Sch.BaseClass::exitRegion();
163 Sch.BaseClass::finishBlock();
177 std::unique_ptr<MachineSchedStrategy> SaveSchedImpl;
187 , SaveMaxRP(R.MaxPressure) {
189 auto BB = R.
Begin->getParent();
190 Sch.BaseClass::startBlock(
BB);
195 Sch.BaseClass::exitRegion();
196 Sch.BaseClass::finishBlock();
198 Sch.
SchedImpl = std::move(SaveSchedImpl);
205 Sch.BaseClass::schedule();
227 bool shouldTrackPressure()
const override {
return false; }
228 bool shouldTrackLaneMasks()
const override {
return false; }
230 SUnit *pickNode(
bool &IsTopNode)
override {
return nullptr; }
231 void schedNode(
SUnit *SU,
bool IsTopNode)
override {}
232 void releaseTopNode(
SUnit *SU)
override {}
233 void releaseBottomNode(
SUnit *SU)
override {}
254 auto const BBEnd = Begin->getParent()->end();
255 auto const BottomMI = End == BBEnd ? std::prev(End) : End;
259 auto AfterBottomMI = std::next(BottomMI);
260 if (AfterBottomMI == BBEnd ||
267 for (
auto I = BottomMI;
I != Begin; --
I)
273 (
dbgs() <<
"Tracked region ",
281 Range &&Schedule)
const {
282 auto const BBEnd = R.
Begin->getParent()->end();
284 if (R.
End != BBEnd) {
291 RPTracker.
reset(*std::prev(BBEnd));
293 for (
auto I = Schedule.end(),
B = Schedule.begin();
I !=
B;) {
304 if (NumRegionInstrs > 2) {
306 new (
Alloc.Allocate())
316 dbgs() <<
"Max RP: ";
317 Regions.back()->MaxPressure.print(
336 std::vector<MachineInstr*>
338 std::vector<MachineInstr*> Res;
339 Res.reserve(Schedule.
size() * 2);
345 for (
auto SU : Schedule) {
346 Res.push_back(SU->getInstr());
347 const auto &
D =
std::find_if(DbgB, DbgE, [SU](decltype(*DbgB) &
P) {
348 return P.second == SU->getInstr();
351 Res.push_back(
D->first);
371 template <
typename Range>
379 auto BB = R.
Begin->getParent();
381 for (
const auto &
I : Schedule) {
386 if (!
MI->isDebugInstr())
389 if (!
MI->isDebugInstr()) {
391 for (
auto &
Op :
MI->operands())
392 if (
Op.isReg() &&
Op.isDef())
393 Op.setIsUndef(
false);
402 Top = std::next(
MI->getIterator());
408 if (!std::is_same<decltype(*Schedule.begin()),
MachineInstr*>::value) {
422 assert((SchedMaxRP == RegionMaxRP && (MaxRP.
empty() || SchedMaxRP == MaxRP))
423 || (
dbgs() <<
"Max RP mismatch!!!\n" 424 "RP for schedule (calculated): ",
425 SchedMaxRP.print(
dbgs(), &
ST),
426 dbgs() <<
"RP for schedule (reported): ",
428 dbgs() <<
"RP after scheduling: ",
429 RegionMaxRP.print(
dbgs(), &
ST),
453 const auto Occ =
Regions.front()->MaxPressure.getOccupancy(
ST);
454 LLVM_DEBUG(
dbgs() <<
"Trying to improve occupancy, target = " << TargetOcc
455 <<
", current = " << Occ <<
'\n');
457 auto NewOcc = TargetOcc;
459 if (R->MaxPressure.getOccupancy(
ST) >= NewOcc)
471 NewOcc = std::min(NewOcc, MaxRP.getOccupancy(
ST));
478 <<
", prev occupancy = " << Occ <<
'\n');
488 bool TryMaximizeOccupancy) {
494 auto Occ =
Regions.front()->MaxPressure.getOccupancy(
ST);
496 if (TryMaximizeOccupancy && Occ < TgtOcc)
501 const int NumPasses = Occ < TgtOcc ? 2 : 1;
503 TgtOcc = std::min(Occ, TgtOcc);
505 "target occupancy = " 508 unsigned FinalOccupancy = std::min(Occ, MFI->getOccupancy());
510 for (
int I = 0;
I < NumPasses; ++
I) {
521 if (
RP.getOccupancy(
ST) < TgtOcc) {
522 LLVM_DEBUG(
dbgs() <<
"Didn't fit into target occupancy O" << TgtOcc);
523 if (R->BestSchedule.get() &&
524 R->BestSchedule->MaxPressure.getOccupancy(
ST) >= TgtOcc) {
530 assert(R->MaxPressure.getOccupancy(
ST) >= TgtOcc);
533 FinalOccupancy = std::min(FinalOccupancy,
RP.getOccupancy(
ST));
536 MFI->limitOccupancy(FinalOccupancy);
548 auto MaxPressure =
Regions.front()->MaxPressure;
550 if (!force && R->MaxPressure.less(
ST, MaxPressure, TgtOcc))
558 dbgs() <<
"\nWarning: Pressure becomes worse after minreg!";
562 if (!force && MaxPressure.less(
ST, RP, TgtOcc))
576 bool TryMaximizeOccupancy) {
582 auto Occ =
Regions.front()->MaxPressure.getOccupancy(
ST);
584 if (TryMaximizeOccupancy && Occ < TgtOcc)
587 TgtOcc = std::min(Occ, TgtOcc);
589 "target occupancy = " 592 unsigned FinalOccupancy = std::min(Occ, MFI->getOccupancy());
600 if (RP.getOccupancy(
ST) < TgtOcc) {
601 LLVM_DEBUG(
dbgs() <<
"Didn't fit into target occupancy O" << TgtOcc);
602 if (R->BestSchedule.get() &&
603 R->BestSchedule->MaxPressure.getOccupancy(
ST) >= TgtOcc) {
610 FinalOccupancy = std::min(FinalOccupancy, RP.getOccupancy(
ST));
613 MFI->limitOccupancy(FinalOccupancy);
unsigned tryMaximizeOccupancy(unsigned TargetOcc=std::numeric_limits< unsigned >::max())
void enterRegion(MachineBasicBlock *BB, MachineBasicBlock::iterator Begin, MachineBasicBlock::iterator End, unsigned RegionInstrs) override
Implement the ScheduleDAGInstrs interface for handling the next scheduling region.
ScheduleDAGTopologicalSort Topo
Topo - A topological ordering for SUnits which permits fast IsReachable and similar queries...
GCNRegPressure getRegionPressure(MachineBasicBlock::iterator Begin, MachineBasicBlock::iterator End) const
AMDGPU specific subclass of TargetSubtarget.
This class represents lattice values for constants.
#define LLVM_DUMP_METHOD
Mark debug helper function definitions like dump() that should not be stripped from debug builds...
This is a minimal scheduler strategy.
void sortRegionsByPressure(unsigned TargetOcc)
decltype(MaxPressure) moveMaxPressure()
SpecificBumpPtrAllocator< Region > Alloc
GCNRegPressure MaxPressure
std::vector< Region * > Regions
void setTargetOccupancy(unsigned Occ)
std::vector< MachineInstr * > detachSchedule(ScheduleRef Schedule) const
unsigned NumRegionInstrs
Instructions in this region (distance(RegionBegin, RegionEnd)).
ScheduleDAGMI is an implementation of ScheduleDAGInstrs that simply schedules machine instructions ac...
std::unique_ptr< MachineSchedStrategy > SchedImpl
ScheduleDAGMILive is an implementation of ScheduleDAGInstrs that schedules machine instructions while...
MachineFunction & MF
Machine function.
void printRegions(raw_ostream &OS) const
MachineBasicBlock::iterator RegionEnd
The end of the range to be scheduled.
Printable printMBBReference(const MachineBasicBlock &MBB)
Prints a machine basic block reference.
MachineBasicBlock::iterator Begin
void finalizeSchedule() override
Allow targets to perform final scheduling actions at the level of the whole MachineFunction.
MachineSchedContext * Context
DbgValueVector DbgValues
Remember instruction that precedes DBG_VALUE.
void buildSchedGraph(AAResults *AA, RegPressureTracker *RPTracker=nullptr, PressureDiffs *PDiffs=nullptr, LiveIntervals *LIS=nullptr, bool TrackLaneMasks=false)
Builds SUnits for the current region.
void InitDAGTopologicalSorting()
Creates the initial topological ordering from the DAG to be scheduled.
void scheduleLegacyMaxOccupancy(bool TryMaximizeOccupancy=true)
void scheduleBest(Region &R)
SlotIndex getRegSlot(bool EC=false) const
Returns the register use/def slot in the current instruction for a normal or early-clobber def...
void increaseOccupancy(const MachineFunction &MF, unsigned Limit)
MachineBasicBlock::iterator RegionBegin
The beginning of the range to be scheduled.
static LLVM_DUMP_METHOD void printLivenessInfo(raw_ostream &OS, MachineBasicBlock::iterator Begin, MachineBasicBlock::iterator End, const LiveIntervals *LIS)
std::vector< const SUnit * > makeMinRegSchedule(ArrayRef< const SUnit *> TopRoots, const ScheduleDAG &DAG)
static void initialize(TargetLibraryInfoImpl &TLI, const Triple &T, ArrayRef< StringLiteral > StandardNames)
Initialize the set of available library functions based on the specified target triple.
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory)...
GCNRPTracker::LiveRegSet getLiveRegsBefore(const MachineInstr &MI, const LiveIntervals &LIS)
void collect(const MachineInstr &MI, const TargetRegisterInfo &TRI, const MachineRegisterInfo &MRI, bool TrackLaneMasks, bool IgnoreDead)
Analyze the given instruction MI and fill in the Uses, Defs and DeadDefs list based on the MachineOpe...
static MachineInstr * getMachineInstr(MachineInstr *MI)
List of registers defined and used by a machine instruction.
bool less(const GCNSubtarget &ST, const GCNRegPressure &O, unsigned MaxOccupancy=std::numeric_limits< unsigned >::max()) const
void printSchedRP(raw_ostream &OS, const GCNRegPressure &Before, const GCNRegPressure &After) const
instr_iterator insert(instr_iterator I, MachineInstr *M)
Insert MI into the instruction list before I, possibly inside a bundle.
unsigned getMinAllowedOccupancy() const
void reset(const MachineInstr &MI, const LiveRegSet *LiveRegs=nullptr)
static GCRegistry::Add< OcamlGC > B("ocaml", "ocaml 3.10-compatible GC")
MachineInstr * getInstr() const
Returns the representative MachineInstr for this SUnit.
void setBestSchedule(Region &R, ScheduleRef Schedule, const GCNRegPressure &MaxRP=GCNRegPressure())
unsigned const MachineRegisterInfo * MRI
const MachineInstr * getLastTrackedMI() const
const TargetSubtargetInfo & getSubtarget() const
getSubtarget - Return the subtarget for which this machine code is being compiled.
unsigned getOccupancy() const
size_t size() const
size - Get the array size.
~OverrideLegacyStrategy()
const unsigned NumRegionInstrs
Ty * getInfo()
getInfo - Keep track of various per-function pieces of information for backends that would like to do...
auto find_if(R &&Range, UnaryPredicate P) -> decltype(adl_begin(Range))
Provide wrappers to std::find_if which take ranges instead of having to pass begin/end explicitly...
const MachineFrameInfo & MFI
GCNRPTracker::LiveRegSet getLiveRegsAfter(const MachineInstr &MI, const LiveIntervals &LIS)
void sort(IteratorTy Start, IteratorTy End)
void adjustLaneLiveness(const LiveIntervals &LIS, const MachineRegisterInfo &MRI, SlotIndex Pos, MachineInstr *AddFlagsMI=nullptr)
Use liveness information to find out which uses/defs are partially undefined/dead and adjust the Regi...
GCNRegPressure getSchedulePressure(const Region &R, Range &&Schedule) const
ArrayRef< SUnit * > getBottomRoots() const
Align max(MaybeAlign Lhs, Align Rhs)
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small...
OverrideLegacyStrategy(Region &R, MachineSchedStrategy &OverrideStrategy, GCNIterativeScheduler &_Sch)
void enterRegion(MachineBasicBlock *bb, MachineBasicBlock::iterator begin, MachineBasicBlock::iterator end, unsigned regioninstrs) override
Implement the ScheduleDAGInstrs interface for handling the next scheduling region.
static GCRegistry::Add< StatepointGC > D("statepoint-example", "an example strategy for statepoint")
void schedule() override
Implement ScheduleDAGInstrs interface for scheduling a sequence of reorderable instructions.
MachineInstr * remove(MachineInstr *I)
Remove the unbundled instruction from the instruction list without deleting it.
raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
GCNUpwardRPTracker UPTracker
const MachineBasicBlock::iterator End
static LLVM_DUMP_METHOD void printRegion(raw_ostream &OS, MachineBasicBlock::iterator Begin, MachineBasicBlock::iterator End, const LiveIntervals *LIS, unsigned MaxInstNum=std::numeric_limits< unsigned >::max())
ArrayRef< const SUnit * > getTopRoots() const
SlotIndex getInstructionIndex(const MachineInstr &Instr) const
Returns the base index of the given instruction.
GCNRegPressure getRegPressure(const MachineRegisterInfo &MRI, Range &&LiveRegs)
Representation of each machine instruction.
This class keeps track of the SPI_SP_INPUT_ADDR config register, which tells the hardware which inter...
BuildDAG(const Region &R, GCNIterativeScheduler &_Sch)
const TargetRegisterInfo * TRI
Target processor register info.
void findRootsAndBiasEdges(SmallVectorImpl< SUnit *> &TopRoots, SmallVectorImpl< SUnit *> &BotRoots)
void printSchedResult(raw_ostream &OS, const Region *R, const GCNRegPressure &RP) const
const StrategyKind Strategy
GCNIterativeScheduler(MachineSchedContext *C, StrategyKind S)
MachineSchedContext provides enough context from the MachineScheduler pass for the target to instanti...
MachineSchedStrategy - Interface to the scheduling algorithm used by ScheduleDAGMI.
void placeDebugValues()
Reinsert debug_values recorded in ScheduleDAGInstrs::DbgValues.
void scheduleRegion(Region &R, Range &&Schedule, const GCNRegPressure &MaxRP=GCNRegPressure())
std::vector< const SUnit * > makeGCNILPScheduler(ArrayRef< const SUnit *> BotRoots, const ScheduleDAG &DAG)
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
void print(raw_ostream &OS, const GCNSubtarget *ST=nullptr) const
void scheduleMinReg(bool force=false)
This class implements an extremely fast bulk output stream that can only output to a stream...
MachineInstr * FirstDbgValue
MachineBasicBlock * BB
The block in which to insert instructions.
void scheduleILP(bool TryMaximizeOccupancy=true)
MachineRegisterInfo & MRI
Virtual/real register map.
std::vector< SUnit > SUnits
The scheduling units.
void recede(const MachineInstr &MI)
RegPressureTracker RPTracker
std::unique_ptr< TentativeSchedule > BestSchedule
void handleMove(MachineInstr &MI, bool UpdateFlags=false)
Call this method to notify LiveIntervals that instruction MI has been moved within a basic block...
Scheduling unit. This is a node in the scheduling DAG.