Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-05-10 08:43:34

0001 //===- ScheduleDAGInstrs.h - MachineInstr Scheduling ------------*- C++ -*-===//
0002 //
0003 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
0004 // See https://llvm.org/LICENSE.txt for license information.
0005 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
0006 //
0007 //===----------------------------------------------------------------------===//
0008 //
0009 /// \file Implements the ScheduleDAGInstrs class, which implements scheduling
0010 /// for a MachineInstr-based dependency graph.
0011 //
0012 //===----------------------------------------------------------------------===//
0013 
0014 #ifndef LLVM_CODEGEN_SCHEDULEDAGINSTRS_H
0015 #define LLVM_CODEGEN_SCHEDULEDAGINSTRS_H
0016 
0017 #include "llvm/ADT/DenseMap.h"
0018 #include "llvm/ADT/PointerIntPair.h"
0019 #include "llvm/ADT/SmallVector.h"
0020 #include "llvm/ADT/SparseMultiSet.h"
0021 #include "llvm/ADT/identity.h"
0022 #include "llvm/Analysis/AliasAnalysis.h"
0023 #include "llvm/CodeGen/LiveRegUnits.h"
0024 #include "llvm/CodeGen/MachineBasicBlock.h"
0025 #include "llvm/CodeGen/ScheduleDAG.h"
0026 #include "llvm/CodeGen/TargetRegisterInfo.h"
0027 #include "llvm/CodeGen/TargetSchedule.h"
0028 #include "llvm/MC/LaneBitmask.h"
0029 #include <cassert>
0030 #include <cstdint>
0031 #include <list>
0032 #include <string>
0033 #include <utility>
0034 #include <vector>
0035 
0036 namespace llvm {
0037 
0038   class AAResults;
0039   class LiveIntervals;
0040   class MachineFrameInfo;
0041   class MachineFunction;
0042   class MachineInstr;
0043   class MachineLoopInfo;
0044   class MachineOperand;
0045   struct MCSchedClassDesc;
0046   class PressureDiffs;
0047   class PseudoSourceValue;
0048   class RegPressureTracker;
0049   class UndefValue;
0050   class Value;
0051 
0052   /// An individual mapping from virtual register number to SUnit.
0053   struct VReg2SUnit {
0054     unsigned VirtReg;
0055     LaneBitmask LaneMask;
0056     SUnit *SU;
0057 
0058     VReg2SUnit(unsigned VReg, LaneBitmask LaneMask, SUnit *SU)
0059       : VirtReg(VReg), LaneMask(LaneMask), SU(SU) {}
0060 
0061     unsigned getSparseSetIndex() const {
0062       return Register::virtReg2Index(VirtReg);
0063     }
0064   };
0065 
0066   /// Mapping from virtual register to SUnit including an operand index.
0067   struct VReg2SUnitOperIdx : public VReg2SUnit {
0068     unsigned OperandIndex;
0069 
0070     VReg2SUnitOperIdx(unsigned VReg, LaneBitmask LaneMask,
0071                       unsigned OperandIndex, SUnit *SU)
0072       : VReg2SUnit(VReg, LaneMask, SU), OperandIndex(OperandIndex) {}
0073   };
0074 
0075   /// Record a physical register access.
0076   /// For non-data-dependent uses, OpIdx == -1.
0077   struct PhysRegSUOper {
0078     SUnit *SU;
0079     int OpIdx;
0080     unsigned RegUnit;
0081 
0082     PhysRegSUOper(SUnit *su, int op, unsigned R)
0083         : SU(su), OpIdx(op), RegUnit(R) {}
0084 
0085     unsigned getSparseSetIndex() const { return RegUnit; }
0086   };
0087 
0088   /// Use a SparseMultiSet to track physical registers. Storage is only
0089   /// allocated once for the pass. It can be cleared in constant time and reused
0090   /// without any frees.
0091   using RegUnit2SUnitsMap =
0092       SparseMultiSet<PhysRegSUOper, identity<unsigned>, uint16_t>;
0093 
0094   /// Track local uses of virtual registers. These uses are gathered by the DAG
0095   /// builder and may be consulted by the scheduler to avoid iterating an entire
0096   /// vreg use list.
0097   using VReg2SUnitMultiMap = SparseMultiSet<VReg2SUnit, VirtReg2IndexFunctor>;
0098 
0099   using VReg2SUnitOperIdxMultiMap =
0100       SparseMultiSet<VReg2SUnitOperIdx, VirtReg2IndexFunctor>;
0101 
0102   using ValueType = PointerUnion<const Value *, const PseudoSourceValue *>;
0103 
0104   struct UnderlyingObject : PointerIntPair<ValueType, 1, bool> {
0105     UnderlyingObject(ValueType V, bool MayAlias)
0106         : PointerIntPair<ValueType, 1, bool>(V, MayAlias) {}
0107 
0108     ValueType getValue() const { return getPointer(); }
0109     bool mayAlias() const { return getInt(); }
0110   };
0111 
0112   using UnderlyingObjectsVector = SmallVector<UnderlyingObject, 4>;
0113 
0114   /// A ScheduleDAG for scheduling lists of MachineInstr.
0115   class ScheduleDAGInstrs : public ScheduleDAG {
0116   protected:
0117     const MachineLoopInfo *MLI = nullptr;
0118     const MachineFrameInfo &MFI;
0119 
0120     /// TargetSchedModel provides an interface to the machine model.
0121     TargetSchedModel SchedModel;
0122 
0123     /// True if the DAG builder should remove kill flags (in preparation for
0124     /// rescheduling).
0125     bool RemoveKillFlags;
0126 
0127     /// The standard DAG builder does not normally include terminators as DAG
0128     /// nodes because it does not create the necessary dependencies to prevent
0129     /// reordering. A specialized scheduler can override
0130     /// TargetInstrInfo::isSchedulingBoundary then enable this flag to indicate
0131     /// it has taken responsibility for scheduling the terminator correctly.
0132     bool CanHandleTerminators = false;
0133 
0134     /// Whether lane masks should get tracked.
0135     bool TrackLaneMasks = false;
0136 
0137     // State specific to the current scheduling region.
0138     // ------------------------------------------------
0139 
0140     /// The block in which to insert instructions
0141     MachineBasicBlock *BB = nullptr;
0142 
0143     /// The beginning of the range to be scheduled.
0144     MachineBasicBlock::iterator RegionBegin;
0145 
0146     /// The end of the range to be scheduled.
0147     MachineBasicBlock::iterator RegionEnd;
0148 
0149     /// Instructions in this region (distance(RegionBegin, RegionEnd)).
0150     unsigned NumRegionInstrs = 0;
0151 
0152     /// After calling BuildSchedGraph, each machine instruction in the current
0153     /// scheduling region is mapped to an SUnit.
0154     DenseMap<MachineInstr*, SUnit*> MISUnitMap;
0155 
0156     // State internal to DAG building.
0157     // -------------------------------
0158 
0159     /// Defs, Uses - Remember where defs and uses of each register are as we
0160     /// iterate upward through the instructions. This is allocated here instead
0161     /// of inside BuildSchedGraph to avoid the need for it to be initialized and
0162     /// destructed for each block.
0163     RegUnit2SUnitsMap Defs;
0164     RegUnit2SUnitsMap Uses;
0165 
0166     /// Tracks the last instruction(s) in this region defining each virtual
0167     /// register. There may be multiple current definitions for a register with
0168     /// disjunct lanemasks.
0169     VReg2SUnitMultiMap CurrentVRegDefs;
0170     /// Tracks the last instructions in this region using each virtual register.
0171     VReg2SUnitOperIdxMultiMap CurrentVRegUses;
0172 
0173     mutable std::optional<BatchAAResults> AAForDep;
0174 
0175     /// Remember a generic side-effecting instruction as we proceed.
0176     /// No other SU ever gets scheduled around it (except in the special
0177     /// case of a huge region that gets reduced).
0178     SUnit *BarrierChain = nullptr;
0179 
0180   public:
0181     /// A list of SUnits, used in Value2SUsMap, during DAG construction.
0182     /// Note: to gain speed it might be worth investigating an optimized
0183     /// implementation of this data structure, such as a singly linked list
0184     /// with a memory pool (SmallVector was tried but slow and SparseSet is not
0185     /// applicable).
0186     using SUList = std::list<SUnit *>;
0187 
0188     /// The direction that should be used to dump the scheduled Sequence.
0189     enum DumpDirection {
0190       TopDown,
0191       BottomUp,
0192       Bidirectional,
0193       NotSet,
0194     };
0195 
0196     void setDumpDirection(DumpDirection D) { DumpDir = D; }
0197 
0198   protected:
0199     DumpDirection DumpDir = NotSet;
0200 
0201     /// A map from ValueType to SUList, used during DAG construction, as
0202     /// a means of remembering which SUs depend on which memory locations.
0203     class Value2SUsMap;
0204 
0205     /// Returns a (possibly null) pointer to the current BatchAAResults.
0206     BatchAAResults *getAAForDep() const {
0207       if (AAForDep.has_value())
0208         return &AAForDep.value();
0209       return nullptr;
0210     }
0211 
0212     /// Reduces maps in FIFO order, by N SUs. This is better than turning
0213     /// every Nth memory SU into BarrierChain in buildSchedGraph(), since
0214     /// it avoids unnecessary edges between seen SUs above the new BarrierChain,
0215     /// and those below it.
0216     void reduceHugeMemNodeMaps(Value2SUsMap &stores,
0217                                Value2SUsMap &loads, unsigned N);
0218 
0219     /// Adds a chain edge between SUa and SUb, but only if both
0220     /// AAResults and Target fail to deny the dependency.
0221     void addChainDependency(SUnit *SUa, SUnit *SUb,
0222                             unsigned Latency = 0);
0223 
0224     /// Adds dependencies as needed from all SUs in list to SU.
0225     void addChainDependencies(SUnit *SU, SUList &SUs, unsigned Latency) {
0226       for (SUnit *Entry : SUs)
0227         addChainDependency(SU, Entry, Latency);
0228     }
0229 
0230     /// Adds dependencies as needed from all SUs in map, to SU.
0231     void addChainDependencies(SUnit *SU, Value2SUsMap &Val2SUsMap);
0232 
0233     /// Adds dependencies as needed to SU, from all SUs mapped to V.
0234     void addChainDependencies(SUnit *SU, Value2SUsMap &Val2SUsMap,
0235                               ValueType V);
0236 
0237     /// Adds barrier chain edges from all SUs in map, and then clear the map.
0238     /// This is equivalent to insertBarrierChain(), but optimized for the common
0239     /// case where the new BarrierChain (a global memory object) has a higher
0240     /// NodeNum than all SUs in map. It is assumed BarrierChain has been set
0241     /// before calling this.
0242     void addBarrierChain(Value2SUsMap &map);
0243 
0244     /// Inserts a barrier chain in a huge region, far below current SU.
0245     /// Adds barrier chain edges from all SUs in map with higher NodeNums than
0246     /// this new BarrierChain, and remove them from map. It is assumed
0247     /// BarrierChain has been set before calling this.
0248     void insertBarrierChain(Value2SUsMap &map);
0249 
0250     /// For an unanalyzable memory access, this Value is used in maps.
0251     UndefValue *UnknownValue;
0252 
0253 
0254     /// Topo - A topological ordering for SUnits which permits fast IsReachable
0255     /// and similar queries.
0256     ScheduleDAGTopologicalSort Topo;
0257 
0258     using DbgValueVector =
0259         std::vector<std::pair<MachineInstr *, MachineInstr *>>;
0260     /// Remember instruction that precedes DBG_VALUE.
0261     /// These are generated by buildSchedGraph but persist so they can be
0262     /// referenced when emitting the final schedule.
0263     DbgValueVector DbgValues;
0264     MachineInstr *FirstDbgValue = nullptr;
0265 
0266     /// Set of live physical registers for updating kill flags.
0267     LiveRegUnits LiveRegs;
0268 
0269   public:
0270     explicit ScheduleDAGInstrs(MachineFunction &mf,
0271                                const MachineLoopInfo *mli,
0272                                bool RemoveKillFlags = false);
0273 
0274     ~ScheduleDAGInstrs() override = default;
0275 
0276     /// Gets the machine model for instruction scheduling.
0277     const TargetSchedModel *getSchedModel() const { return &SchedModel; }
0278 
0279     /// Resolves and cache a resolved scheduling class for an SUnit.
0280     const MCSchedClassDesc *getSchedClass(SUnit *SU) const {
0281       if (!SU->SchedClass && SchedModel.hasInstrSchedModel())
0282         SU->SchedClass = SchedModel.resolveSchedClass(SU->getInstr());
0283       return SU->SchedClass;
0284     }
0285 
0286     /// IsReachable - Checks if SU is reachable from TargetSU.
0287     bool IsReachable(SUnit *SU, SUnit *TargetSU) {
0288       return Topo.IsReachable(SU, TargetSU);
0289     }
0290 
0291     /// Returns an iterator to the top of the current scheduling region.
0292     MachineBasicBlock::iterator begin() const { return RegionBegin; }
0293 
0294     /// Returns an iterator to the bottom of the current scheduling region.
0295     MachineBasicBlock::iterator end() const { return RegionEnd; }
0296 
0297     /// Creates a new SUnit and return a ptr to it.
0298     SUnit *newSUnit(MachineInstr *MI);
0299 
0300     /// Returns an existing SUnit for this MI, or nullptr.
0301     SUnit *getSUnit(MachineInstr *MI) const;
0302 
0303     /// If this method returns true, handling of the scheduling regions
0304     /// themselves (in case of a scheduling boundary in MBB) will be done
0305     /// beginning with the topmost region of MBB.
0306     virtual bool doMBBSchedRegionsTopDown() const { return false; }
0307 
0308     /// Prepares to perform scheduling in the given block.
0309     virtual void startBlock(MachineBasicBlock *BB);
0310 
0311     /// Cleans up after scheduling in the given block.
0312     virtual void finishBlock();
0313 
0314     /// Initialize the DAG and common scheduler state for a new
0315     /// scheduling region. This does not actually create the DAG, only clears
0316     /// it. The scheduling driver may call BuildSchedGraph multiple times per
0317     /// scheduling region.
0318     virtual void enterRegion(MachineBasicBlock *bb,
0319                              MachineBasicBlock::iterator begin,
0320                              MachineBasicBlock::iterator end,
0321                              unsigned regioninstrs);
0322 
0323     /// Called when the scheduler has finished scheduling the current region.
0324     virtual void exitRegion();
0325 
0326     /// Builds SUnits for the current region.
0327     /// If \p RPTracker is non-null, compute register pressure as a side effect.
0328     /// The DAG builder is an efficient place to do it because it already visits
0329     /// operands.
0330     void buildSchedGraph(AAResults *AA,
0331                          RegPressureTracker *RPTracker = nullptr,
0332                          PressureDiffs *PDiffs = nullptr,
0333                          LiveIntervals *LIS = nullptr,
0334                          bool TrackLaneMasks = false);
0335 
0336     /// Adds dependencies from instructions in the current list of
0337     /// instructions being scheduled to scheduling barrier. We want to make sure
0338     /// instructions which define registers that are either used by the
0339     /// terminator or are live-out are properly scheduled. This is especially
0340     /// important when the definition latency of the return value(s) are too
0341     /// high to be hidden by the branch or when the liveout registers used by
0342     /// instructions in the fallthrough block.
0343     void addSchedBarrierDeps();
0344 
0345     /// Orders nodes according to selected style.
0346     ///
0347     /// Typically, a scheduling algorithm will implement schedule() without
0348     /// overriding enterRegion() or exitRegion().
0349     virtual void schedule() = 0;
0350 
0351     /// Allow targets to perform final scheduling actions at the level of the
0352     /// whole MachineFunction. By default does nothing.
0353     virtual void finalizeSchedule() {}
0354 
0355     void dumpNode(const SUnit &SU) const override;
0356     void dump() const override;
0357 
0358     /// Returns a label for a DAG node that points to an instruction.
0359     std::string getGraphNodeLabel(const SUnit *SU) const override;
0360 
0361     /// Returns a label for the region of code covered by the DAG.
0362     std::string getDAGName() const override;
0363 
0364     /// Fixes register kill flags that scheduling has made invalid.
0365     void fixupKills(MachineBasicBlock &MBB);
0366 
0367     /// True if an edge can be added from PredSU to SuccSU without creating
0368     /// a cycle.
0369     bool canAddEdge(SUnit *SuccSU, SUnit *PredSU);
0370 
0371     /// Add a DAG edge to the given SU with the given predecessor
0372     /// dependence data.
0373     ///
0374     /// \returns true if the edge may be added without creating a cycle OR if an
0375     /// equivalent edge already existed (false indicates failure).
0376     bool addEdge(SUnit *SuccSU, const SDep &PredDep);
0377 
0378   protected:
0379     void initSUnits();
0380     void addPhysRegDataDeps(SUnit *SU, unsigned OperIdx);
0381     void addPhysRegDeps(SUnit *SU, unsigned OperIdx);
0382     void addVRegDefDeps(SUnit *SU, unsigned OperIdx);
0383     void addVRegUseDeps(SUnit *SU, unsigned OperIdx);
0384 
0385     /// Returns a mask for which lanes get read/written by the given (register)
0386     /// machine operand.
0387     LaneBitmask getLaneMaskForMO(const MachineOperand &MO) const;
0388 
0389     /// Returns true if the def register in \p MO has no uses.
0390     bool deadDefHasNoUse(const MachineOperand &MO);
0391   };
0392 
0393   /// Creates a new SUnit and return a ptr to it.
0394   inline SUnit *ScheduleDAGInstrs::newSUnit(MachineInstr *MI) {
0395 #ifndef NDEBUG
0396     const SUnit *Addr = SUnits.empty() ? nullptr : &SUnits[0];
0397 #endif
0398     SUnits.emplace_back(MI, (unsigned)SUnits.size());
0399     assert((Addr == nullptr || Addr == &SUnits[0]) &&
0400            "SUnits std::vector reallocated on the fly!");
0401     return &SUnits.back();
0402   }
0403 
0404   /// Returns an existing SUnit for this MI, or nullptr.
0405   inline SUnit *ScheduleDAGInstrs::getSUnit(MachineInstr *MI) const {
0406     return MISUnitMap.lookup(MI);
0407   }
0408 
0409 } // end namespace llvm
0410 
0411 #endif // LLVM_CODEGEN_SCHEDULEDAGINSTRS_H