LLVM  5.0.0
ScheduleDAGInstrs.h
Go to the documentation of this file.
1 //===- ScheduleDAGInstrs.h - MachineInstr Scheduling ------------*- C++ -*-===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 /// \file Implements the ScheduleDAGInstrs class, which implements scheduling
11 /// for a MachineInstr-based dependency graph.
12 //
13 //===----------------------------------------------------------------------===//
14 
15 #ifndef LLVM_CODEGEN_SCHEDULEDAGINSTRS_H
16 #define LLVM_CODEGEN_SCHEDULEDAGINSTRS_H
17 
18 #include "llvm/ADT/DenseMap.h"
20 #include "llvm/ADT/STLExtras.h"
21 #include "llvm/ADT/SmallVector.h"
23 #include "llvm/ADT/SparseSet.h"
28 #include "llvm/MC/LaneBitmask.h"
30 #include <cassert>
31 #include <cstdint>
32 #include <list>
33 #include <utility>
34 #include <vector>
35 
36 namespace llvm {
37 
38  class LiveIntervals;
39  class MachineFrameInfo;
40  class MachineFunction;
41  class MachineInstr;
42  class MachineLoopInfo;
43  class MachineOperand;
44  struct MCSchedClassDesc;
45  class PressureDiffs;
46  class PseudoSourceValue;
47  class RegPressureTracker;
48  class UndefValue;
49  class Value;
50 
51  /// An individual mapping from virtual register number to SUnit.
52  struct VReg2SUnit {
53  unsigned VirtReg;
56 
58  : VirtReg(VReg), LaneMask(LaneMask), SU(SU) {}
59 
60  unsigned getSparseSetIndex() const {
62  }
63  };
64 
65  /// Mapping from virtual register to SUnit including an operand index.
66  struct VReg2SUnitOperIdx : public VReg2SUnit {
67  unsigned OperandIndex;
68 
70  unsigned OperandIndex, SUnit *SU)
71  : VReg2SUnit(VReg, LaneMask, SU), OperandIndex(OperandIndex) {}
72  };
73 
74  /// Record a physical register access.
75  /// For non-data-dependent uses, OpIdx == -1.
76  struct PhysRegSUOper {
78  int OpIdx;
79  unsigned Reg;
80 
81  PhysRegSUOper(SUnit *su, int op, unsigned R): SU(su), OpIdx(op), Reg(R) {}
82 
83  unsigned getSparseSetIndex() const { return Reg; }
84  };
85 
86  /// Use a SparseMultiSet to track physical registers. Storage is only
87  /// allocated once for the pass. It can be cleared in constant time and reused
88  /// without any frees.
89  using Reg2SUnitsMap =
91 
92  /// Use SparseSet as a SparseMap by relying on the fact that it never
93  /// compares ValueT's, only unsigned keys. This allows the set to be cleared
94  /// between scheduling regions in constant time as long as ValueT does not
95  /// require a destructor.
97 
98  /// Track local uses of virtual registers. These uses are gathered by the DAG
99  /// builder and may be consulted by the scheduler to avoid iterating an entire
100  /// vreg use list.
102 
105 
107 
108  struct UnderlyingObject : PointerIntPair<ValueType, 1, bool> {
110  : PointerIntPair<ValueType, 1, bool>(V, MayAlias) {}
111 
112  ValueType getValue() const { return getPointer(); }
113  bool mayAlias() const { return getInt(); }
114  };
115 
117 
118  /// A ScheduleDAG for scheduling lists of MachineInstr.
120  protected:
123 
124  /// TargetSchedModel provides an interface to the machine model.
126 
127  /// True if the DAG builder should remove kill flags (in preparation for
128  /// rescheduling).
130 
131  /// The standard DAG builder does not normally include terminators as DAG
132  /// nodes because it does not create the necessary dependencies to prevent
133  /// reordering. A specialized scheduler can override
134  /// TargetInstrInfo::isSchedulingBoundary then enable this flag to indicate
135  /// it has taken responsibility for scheduling the terminator correctly.
136  bool CanHandleTerminators = false;
137 
138  /// Whether lane masks should get tracked.
139  bool TrackLaneMasks = false;
140 
141  // State specific to the current scheduling region.
142  // ------------------------------------------------
143 
144  /// The block in which to insert instructions
146 
147  /// The beginning of the range to be scheduled.
149 
150  /// The end of the range to be scheduled.
152 
153  /// Instructions in this region (distance(RegionBegin, RegionEnd)).
154  unsigned NumRegionInstrs;
155 
156  /// After calling BuildSchedGraph, each machine instruction in the current
157  /// scheduling region is mapped to an SUnit.
159 
160  // State internal to DAG building.
161  // -------------------------------
162 
163  /// Defs, Uses - Remember where defs and uses of each register are as we
164  /// iterate upward through the instructions. This is allocated here instead
165  /// of inside BuildSchedGraph to avoid the need for it to be initialized and
166  /// destructed for each block.
169 
170  /// Tracks the last instruction(s) in this region defining each virtual
171  /// register. There may be multiple current definitions for a register with
172  /// disjunct lanemasks.
174  /// Tracks the last instructions in this region using each virtual register.
176 
178 
179  /// Remember a generic side-effecting instruction as we proceed.
180  /// No other SU ever gets scheduled around it (except in the special
181  /// case of a huge region that gets reduced).
182  SUnit *BarrierChain = nullptr;
183 
184  public:
185  /// A list of SUnits, used in Value2SUsMap, during DAG construction.
186  /// Note: to gain speed it might be worth investigating an optimized
187  /// implementation of this data structure, such as a singly linked list
188  /// with a memory pool (SmallVector was tried but slow and SparseSet is not
189  /// applicable).
190  using SUList = std::list<SUnit *>;
191 
192  protected:
193  /// \brief A map from ValueType to SUList, used during DAG construction, as
194  /// a means of remembering which SUs depend on which memory locations.
195  class Value2SUsMap;
196 
197  /// Reduces maps in FIFO order, by N SUs. This is better than turning
198  /// every Nth memory SU into BarrierChain in buildSchedGraph(), since
199  /// it avoids unnecessary edges between seen SUs above the new BarrierChain,
200  /// and those below it.
202  Value2SUsMap &loads, unsigned N);
203 
204  /// \brief Adds a chain edge between SUa and SUb, but only if both
205  /// AliasAnalysis and Target fail to deny the dependency.
206  void addChainDependency(SUnit *SUa, SUnit *SUb,
207  unsigned Latency = 0);
208 
209  /// Adds dependencies as needed from all SUs in list to SU.
210  void addChainDependencies(SUnit *SU, SUList &SUs, unsigned Latency) {
211  for (SUnit *Entry : SUs)
212  addChainDependency(SU, Entry, Latency);
213  }
214 
215  /// Adds dependencies as needed from all SUs in map, to SU.
216  void addChainDependencies(SUnit *SU, Value2SUsMap &Val2SUsMap);
217 
218  /// Adds dependencies as needed to SU, from all SUs mapped to V.
219  void addChainDependencies(SUnit *SU, Value2SUsMap &Val2SUsMap,
220  ValueType V);
221 
222  /// Adds barrier chain edges from all SUs in map, and then clear the map.
223  /// This is equivalent to insertBarrierChain(), but optimized for the common
224  /// case where the new BarrierChain (a global memory object) has a higher
225  /// NodeNum than all SUs in map. It is assumed BarrierChain has been set
226  /// before calling this.
227  void addBarrierChain(Value2SUsMap &map);
228 
229  /// Inserts a barrier chain in a huge region, far below current SU.
230  /// Adds barrier chain edges from all SUs in map with higher NodeNums than
231  /// this new BarrierChain, and remove them from map. It is assumed
232  /// BarrierChain has been set before calling this.
233  void insertBarrierChain(Value2SUsMap &map);
234 
235  /// For an unanalyzable memory access, this Value is used in maps.
237 
238  using DbgValueVector =
239  std::vector<std::pair<MachineInstr *, MachineInstr *>>;
240  /// Remember instruction that precedes DBG_VALUE.
241  /// These are generated by buildSchedGraph but persist so they can be
242  /// referenced when emitting the final schedule.
245 
246  /// Set of live physical registers for updating kill flags.
248 
249  public:
250  explicit ScheduleDAGInstrs(MachineFunction &mf,
251  const MachineLoopInfo *mli,
252  bool RemoveKillFlags = false);
253 
254  ~ScheduleDAGInstrs() override = default;
255 
256  /// Gets the machine model for instruction scheduling.
257  const TargetSchedModel *getSchedModel() const { return &SchedModel; }
258 
259  /// Resolves and cache a resolved scheduling class for an SUnit.
263  return SU->SchedClass;
264  }
265 
266  /// Returns an iterator to the top of the current scheduling region.
268 
269  /// Returns an iterator to the bottom of the current scheduling region.
271 
272  /// Creates a new SUnit and return a ptr to it.
274 
275  /// Returns an existing SUnit for this MI, or nullptr.
276  SUnit *getSUnit(MachineInstr *MI) const;
277 
278  /// Prepares to perform scheduling in the given block.
279  virtual void startBlock(MachineBasicBlock *BB);
280 
281  /// Cleans up after scheduling in the given block.
282  virtual void finishBlock();
283 
284  /// \brief Initialize the DAG and common scheduler state for a new
285  /// scheduling region. This does not actually create the DAG, only clears
286  /// it. The scheduling driver may call BuildSchedGraph multiple times per
287  /// scheduling region.
288  virtual void enterRegion(MachineBasicBlock *bb,
291  unsigned regioninstrs);
292 
293  /// Called when the scheduler has finished scheduling the current region.
294  virtual void exitRegion();
295 
296  /// Builds SUnits for the current region.
297  /// If \p RPTracker is non-null, compute register pressure as a side effect.
298  /// The DAG builder is an efficient place to do it because it already visits
299  /// operands.
301  RegPressureTracker *RPTracker = nullptr,
302  PressureDiffs *PDiffs = nullptr,
303  LiveIntervals *LIS = nullptr,
304  bool TrackLaneMasks = false);
305 
306  /// \brief Adds dependencies from instructions in the current list of
307  /// instructions being scheduled to scheduling barrier. We want to make sure
308  /// instructions which define registers that are either used by the
309  /// terminator or are live-out are properly scheduled. This is especially
310  /// important when the definition latency of the return value(s) are too
311  /// high to be hidden by the branch or when the liveout registers used by
312  /// instructions in the fallthrough block.
313  void addSchedBarrierDeps();
314 
315  /// Orders nodes according to selected style.
316  ///
317  /// Typically, a scheduling algorithm will implement schedule() without
318  /// overriding enterRegion() or exitRegion().
319  virtual void schedule() = 0;
320 
321  /// Allow targets to perform final scheduling actions at the level of the
322  /// whole MachineFunction. By default does nothing.
323  virtual void finalizeSchedule() {}
324 
325  void dumpNode(const SUnit *SU) const override;
326 
327  /// Returns a label for a DAG node that points to an instruction.
328  std::string getGraphNodeLabel(const SUnit *SU) const override;
329 
330  /// Returns a label for the region of code covered by the DAG.
331  std::string getDAGName() const override;
332 
333  /// Fixes register kill flags that scheduling has made invalid.
334  void fixupKills(MachineBasicBlock &MBB);
335 
336  protected:
337  void initSUnits();
338  void addPhysRegDataDeps(SUnit *SU, unsigned OperIdx);
339  void addPhysRegDeps(SUnit *SU, unsigned OperIdx);
340  void addVRegDefDeps(SUnit *SU, unsigned OperIdx);
341  void addVRegUseDeps(SUnit *SU, unsigned OperIdx);
342 
343  /// Initializes register live-range state for updating kills.
344  /// PostRA helper for rewriting kill flags.
346 
347  /// Toggles a register operand kill flag.
348  ///
349  /// Other adjustments may be made to the instruction if necessary. Return
350  /// true if the operand has been deleted, false if not.
352 
353  /// Returns a mask for which lanes get read/written by the given (register)
354  /// machine operand.
356  };
357 
358  /// Creates a new SUnit and return a ptr to it.
360 #ifndef NDEBUG
361  const SUnit *Addr = SUnits.empty() ? nullptr : &SUnits[0];
362 #endif
363  SUnits.emplace_back(MI, (unsigned)SUnits.size());
364  assert((Addr == nullptr || Addr == &SUnits[0]) &&
365  "SUnits std::vector reallocated on the fly!");
366  return &SUnits.back();
367  }
368 
369  /// Returns an existing SUnit for this MI, or nullptr.
372  if (I == MISUnitMap.end())
373  return nullptr;
374  return I->second;
375  }
376 
377 } // end namespace llvm
378 
379 #endif // LLVM_CODEGEN_SCHEDULEDAGINSTRS_H
virtual void finishBlock()
Cleans up after scheduling in the given block.
A common definition of LaneBitmask for use in TableGen and CodeGen.
const MCSchedClassDesc * resolveSchedClass(const MachineInstr *MI) const
Return the MCSchedClassDesc for this instruction.
unsigned getSparseSetIndex() const
static unsigned virtReg2Index(unsigned Reg)
Convert a virtual register number to a 0-based index.
Record a physical register access.
void addVRegDefDeps(SUnit *SU, unsigned OperIdx)
Adds register output and data dependencies from this SUnit to instructions that occur later in the sa...
LivePhysRegs LiveRegs
Set of live physical registers for updating kill flags.
bool TrackLaneMasks
Whether lane masks should get tracked.
void dumpNode(const SUnit *SU) const override
MachineInstr * getInstr() const
Returns the representative MachineInstr for this SUnit.
Definition: ScheduleDAG.h:378
TargetSchedModel SchedModel
TargetSchedModel provides an interface to the machine model.
void fixupKills(MachineBasicBlock &MBB)
Fixes register kill flags that scheduling has made invalid.
bool CanHandleTerminators
The standard DAG builder does not normally include terminators as DAG nodes because it does not creat...
MachineBasicBlock::iterator begin() const
Returns an iterator to the top of the current scheduling region.
const MCSchedClassDesc * getSchedClass(SUnit *SU) const
Resolves and cache a resolved scheduling class for an SUnit.
#define op(i)
unsigned NumRegionInstrs
Instructions in this region (distance(RegionBegin, RegionEnd)).
void addPhysRegDataDeps(SUnit *SU, unsigned OperIdx)
MO is an operand of SU's instruction that defines a physical register.
virtual void startBlock(MachineBasicBlock *BB)
Prepares to perform scheduling in the given block.
The two locations may or may not alias. This is the least precise result.
Definition: AliasAnalysis.h:81
void insertBarrierChain(Value2SUsMap &map)
Inserts a barrier chain in a huge region, far below current SU.
const TargetSchedModel * getSchedModel() const
Gets the machine model for instruction scheduling.
void buildSchedGraph(AliasAnalysis *AA, RegPressureTracker *RPTracker=nullptr, PressureDiffs *PDiffs=nullptr, LiveIntervals *LIS=nullptr, bool TrackLaneMasks=false)
Builds SUnits for the current region.
MachineBasicBlock::iterator RegionEnd
The end of the range to be scheduled.
DenseMap< MachineInstr *, SUnit * > MISUnitMap
After calling BuildSchedGraph, each machine instruction in the current scheduling region is mapped to...
void addSchedBarrierDeps()
Adds dependencies from instructions in the current list of instructions being scheduled to scheduling...
Provide an instruction scheduling machine model to CodeGen passes.
'undef' values are things that do not have specified contents.
Definition: Constants.h:1247
An individual mapping from virtual register number to SUnit.
std::vector< std::pair< MachineInstr *, MachineInstr * >> DbgValueVector
The MachineFrameInfo class represents an abstract stack frame until prolog/epilog code is inserted...
DbgValueVector DbgValues
Remember instruction that precedes DBG_VALUE.
void startBlockForKills(MachineBasicBlock *BB)
Initializes register live-range state for updating kills.
MachineBasicBlock::iterator RegionBegin
The beginning of the range to be scheduled.
void addVRegUseDeps(SUnit *SU, unsigned OperIdx)
Adds a register data dependency if the instruction that defines the virtual register used at OperIdx ...
~ScheduleDAGInstrs() override=default
SUnit * BarrierChain
Remember a generic side-effecting instruction as we proceed.
virtual void enterRegion(MachineBasicBlock *bb, MachineBasicBlock::iterator begin, MachineBasicBlock::iterator end, unsigned regioninstrs)
Initialize the DAG and common scheduler state for a new scheduling region.
std::list< SUnit * > SUList
A list of SUnits, used in Value2SUsMap, during DAG construction.
Array of PressureDiffs.
PointerIntPair - This class implements a pair of a pointer and small integer.
bool RemoveKillFlags
True if the DAG builder should remove kill flags (in preparation for rescheduling).
Summarize the scheduling resources required for an instruction of a particular scheduling class...
Definition: MCSchedule.h:101
Track the current register pressure at some position in the instruction stream, and remember the high...
SparseMultiSet< PhysRegSUOper, identity< unsigned >, uint16_t > Reg2SUnitsMap
Use a SparseMultiSet to track physical registers.
virtual void exitRegion()
Called when the scheduler has finished scheduling the current region.
const MCSchedClassDesc * SchedClass
nullptr or resolved SchedClass.
Definition: ScheduleDAG.h:258
const MachineFrameInfo & MFI
This file implements the LivePhysRegs utility for tracking liveness of physical registers.
std::string getDAGName() const override
Returns a label for the region of code covered by the DAG.
void addPhysRegDeps(SUnit *SU, unsigned OperIdx)
Adds register dependencies (data, anti, and output) from this SUnit to following instructions in the ...
bool hasInstrSchedModel() const
Return true if this machine model includes an instruction-level scheduling model. ...
std::string getGraphNodeLabel(const SUnit *SU) const override
Returns a label for a DAG node that points to an instruction.
PhysRegSUOper(SUnit *su, int op, unsigned R)
void reduceHugeMemNodeMaps(Value2SUsMap &stores, Value2SUsMap &loads, unsigned N)
Reduces maps in FIFO order, by N SUs.
ValueType getValue() const
UndefValue * UnknownValue
For an unanalyzable memory access, this Value is used in maps.
MachineOperand class - Representation of each machine instruction operand.
LaneBitmask LaneMask
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small...
Definition: SmallVector.h:861
VReg2SUnitOperIdx(unsigned VReg, LaneBitmask LaneMask, unsigned OperandIndex, SUnit *SU)
Reg2SUnitsMap Defs
Defs, Uses - Remember where defs and uses of each register are as we iterate upward through the instr...
const MachineLoopInfo * MLI
const size_t N
VReg2SUnitOperIdxMultiMap CurrentVRegUses
Tracks the last instructions in this region using each virtual register.
virtual void finalizeSchedule()
Allow targets to perform final scheduling actions at the level of the whole MachineFunction.
virtual void schedule()=0
Orders nodes according to selected style.
MachineBasicBlock::iterator end() const
Returns an iterator to the bottom of the current scheduling region.
VReg2SUnit(unsigned VReg, LaneBitmask LaneMask, SUnit *SU)
SUnit * getSUnit(MachineInstr *MI) const
Returns an existing SUnit for this MI, or nullptr.
A ScheduleDAG for scheduling lists of MachineInstr.
Representation of each machine instruction.
Definition: MachineInstr.h:59
SparseSet - Fast set implmentation for objects that can be identified by small unsigned keys...
Definition: SparseSet.h:123
A set of physical registers with utility functions to track liveness when walking backward/forward th...
Definition: LivePhysRegs.h:49
void addChainDependencies(SUnit *SU, SUList &SUs, unsigned Latency)
Adds dependencies as needed from all SUs in list to SU.
#define I(x, y, z)
Definition: MD5.cpp:58
Mapping from virtual register to SUnit including an operand index.
void addChainDependency(SUnit *SUa, SUnit *SUb, unsigned Latency=0)
Adds a chain edge between SUa and SUb, but only if both AliasAnalysis and Target fail to deny the dep...
unsigned getSparseSetIndex() const
ScheduleDAGInstrs(MachineFunction &mf, const MachineLoopInfo *mli, bool RemoveKillFlags=false)
SUnit * newSUnit(MachineInstr *MI)
Creates a new SUnit and return a ptr to it.
void addBarrierChain(Value2SUsMap &map)
Adds barrier chain edges from all SUs in map, and then clear the map.
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
LaneBitmask getLaneMaskForMO(const MachineOperand &MO) const
Returns a mask for which lanes get read/written by the given (register) machine operand.
void initSUnits()
Creates an SUnit for each real instruction, numbered in top-down topological order.
Fast multiset implementation for objects that can be identified by small unsigned keys...
UnderlyingObject(ValueType V, bool MayAlias)
MachineBasicBlock * BB
The block in which to insert instructions.
IRTranslator LLVM IR MI
std::vector< SUnit > SUnits
The scheduling units.
Definition: ScheduleDAG.h:572
VReg2SUnitMultiMap CurrentVRegDefs
Tracks the last instruction(s) in this region defining each virtual register.
hexagon widen stores
Scheduling unit. This is a node in the scheduling DAG.
Definition: ScheduleDAG.h:247
void toggleKillFlag(MachineInstr &MI, MachineOperand &MO)
Toggles a register operand kill flag.