10b57cec5SDimitry Andric //===---- ScheduleDAGSDNodes.h - SDNode Scheduling --------------*- C++ -*-===// 20b57cec5SDimitry Andric // 30b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 40b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information. 50b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 60b57cec5SDimitry Andric // 70b57cec5SDimitry Andric //===----------------------------------------------------------------------===// 80b57cec5SDimitry Andric // 90b57cec5SDimitry Andric // This file implements the ScheduleDAGSDNodes class, which implements 100b57cec5SDimitry Andric // scheduling for an SDNode-based dependency graph. 110b57cec5SDimitry Andric // 120b57cec5SDimitry Andric //===----------------------------------------------------------------------===// 130b57cec5SDimitry Andric 140b57cec5SDimitry Andric #ifndef LLVM_LIB_CODEGEN_SELECTIONDAG_SCHEDULEDAGSDNODES_H 150b57cec5SDimitry Andric #define LLVM_LIB_CODEGEN_SELECTIONDAG_SCHEDULEDAGSDNODES_H 160b57cec5SDimitry Andric 170b57cec5SDimitry Andric #include "llvm/CodeGen/ISDOpcodes.h" 180b57cec5SDimitry Andric #include "llvm/CodeGen/MachineBasicBlock.h" 190b57cec5SDimitry Andric #include "llvm/CodeGen/ScheduleDAG.h" 200b57cec5SDimitry Andric #include "llvm/CodeGen/SelectionDAGNodes.h" 21*0fca6ea1SDimitry Andric #include "llvm/CodeGenTypes/MachineValueType.h" 220b57cec5SDimitry Andric #include "llvm/Support/Casting.h" 230b57cec5SDimitry Andric #include <cassert> 240b57cec5SDimitry Andric #include <string> 250b57cec5SDimitry Andric #include <vector> 260b57cec5SDimitry Andric 270b57cec5SDimitry Andric namespace llvm { 280b57cec5SDimitry Andric 298bcb0991SDimitry Andric class AAResults; 300b57cec5SDimitry Andric class InstrItineraryData; 310b57cec5SDimitry Andric 320b57cec5SDimitry Andric /// ScheduleDAGSDNodes - A ScheduleDAG for scheduling SDNode-based DAGs. 330b57cec5SDimitry Andric /// 340b57cec5SDimitry Andric /// Edges between SUnits are initially based on edges in the SelectionDAG, 350b57cec5SDimitry Andric /// and additional edges can be added by the schedulers as heuristics. 360b57cec5SDimitry Andric /// SDNodes such as Constants, Registers, and a few others that are not 370b57cec5SDimitry Andric /// interesting to schedulers are not allocated SUnits. 380b57cec5SDimitry Andric /// 390b57cec5SDimitry Andric /// SDNodes with MVT::Glue operands are grouped along with the flagged 400b57cec5SDimitry Andric /// nodes into a single SUnit so that they are scheduled together. 410b57cec5SDimitry Andric /// 420b57cec5SDimitry Andric /// SDNode-based scheduling graphs do not use SDep::Anti or SDep::Output 430b57cec5SDimitry Andric /// edges. Physical register dependence information is not carried in 440b57cec5SDimitry Andric /// the DAG and must be handled explicitly by schedulers. 450b57cec5SDimitry Andric /// 460b57cec5SDimitry Andric class ScheduleDAGSDNodes : public ScheduleDAG { 470b57cec5SDimitry Andric public: 481fd87a68SDimitry Andric MachineBasicBlock *BB = nullptr; 491fd87a68SDimitry Andric SelectionDAG *DAG = nullptr; // DAG of the current basic block 500b57cec5SDimitry Andric const InstrItineraryData *InstrItins; 510b57cec5SDimitry Andric 520b57cec5SDimitry Andric /// The schedule. Null SUnit*'s represent noop instructions. 530b57cec5SDimitry Andric std::vector<SUnit*> Sequence; 540b57cec5SDimitry Andric 550b57cec5SDimitry Andric explicit ScheduleDAGSDNodes(MachineFunction &mf); 560b57cec5SDimitry Andric 570b57cec5SDimitry Andric ~ScheduleDAGSDNodes() override = default; 580b57cec5SDimitry Andric 590b57cec5SDimitry Andric /// Run - perform scheduling. 600b57cec5SDimitry Andric /// 610b57cec5SDimitry Andric void Run(SelectionDAG *dag, MachineBasicBlock *bb); 620b57cec5SDimitry Andric 630b57cec5SDimitry Andric /// isPassiveNode - Return true if the node is a non-scheduled leaf. 640b57cec5SDimitry Andric /// isPassiveNode(SDNode * Node)650b57cec5SDimitry Andric static bool isPassiveNode(SDNode *Node) { 660b57cec5SDimitry Andric if (isa<ConstantSDNode>(Node)) return true; 670b57cec5SDimitry Andric if (isa<ConstantFPSDNode>(Node)) return true; 680b57cec5SDimitry Andric if (isa<RegisterSDNode>(Node)) return true; 690b57cec5SDimitry Andric if (isa<RegisterMaskSDNode>(Node)) return true; 700b57cec5SDimitry Andric if (isa<GlobalAddressSDNode>(Node)) return true; 710b57cec5SDimitry Andric if (isa<BasicBlockSDNode>(Node)) return true; 720b57cec5SDimitry Andric if (isa<FrameIndexSDNode>(Node)) return true; 730b57cec5SDimitry Andric if (isa<ConstantPoolSDNode>(Node)) return true; 740b57cec5SDimitry Andric if (isa<TargetIndexSDNode>(Node)) return true; 750b57cec5SDimitry Andric if (isa<JumpTableSDNode>(Node)) return true; 760b57cec5SDimitry Andric if (isa<ExternalSymbolSDNode>(Node)) return true; 770b57cec5SDimitry Andric if (isa<MCSymbolSDNode>(Node)) return true; 780b57cec5SDimitry Andric if (isa<BlockAddressSDNode>(Node)) return true; 790b57cec5SDimitry Andric if (Node->getOpcode() == ISD::EntryToken || 800b57cec5SDimitry Andric isa<MDNodeSDNode>(Node)) return true; 810b57cec5SDimitry Andric return false; 820b57cec5SDimitry Andric } 830b57cec5SDimitry Andric 840b57cec5SDimitry Andric /// NewSUnit - Creates a new SUnit and return a ptr to it. 850b57cec5SDimitry Andric /// 860b57cec5SDimitry Andric SUnit *newSUnit(SDNode *N); 870b57cec5SDimitry Andric 880b57cec5SDimitry Andric /// Clone - Creates a clone of the specified SUnit. It does not copy the 890b57cec5SDimitry Andric /// predecessors / successors info nor the temporary scheduling states. 900b57cec5SDimitry Andric /// 910b57cec5SDimitry Andric SUnit *Clone(SUnit *Old); 920b57cec5SDimitry Andric 930b57cec5SDimitry Andric /// BuildSchedGraph - Build the SUnit graph from the selection dag that we 940b57cec5SDimitry Andric /// are input. This SUnit graph is similar to the SelectionDAG, but 950b57cec5SDimitry Andric /// excludes nodes that aren't interesting to scheduling, and represents 960b57cec5SDimitry Andric /// flagged together nodes with a single SUnit. 978bcb0991SDimitry Andric void BuildSchedGraph(AAResults *AA); 980b57cec5SDimitry Andric 990b57cec5SDimitry Andric /// InitNumRegDefsLeft - Determine the # of regs defined by this node. 1000b57cec5SDimitry Andric /// 1010b57cec5SDimitry Andric void InitNumRegDefsLeft(SUnit *SU); 1020b57cec5SDimitry Andric 1030b57cec5SDimitry Andric /// computeLatency - Compute node latency. 1040b57cec5SDimitry Andric /// 1050b57cec5SDimitry Andric virtual void computeLatency(SUnit *SU); 1060b57cec5SDimitry Andric 1070b57cec5SDimitry Andric virtual void computeOperandLatency(SDNode *Def, SDNode *Use, 1080b57cec5SDimitry Andric unsigned OpIdx, SDep& dep) const; 1090b57cec5SDimitry Andric 1100b57cec5SDimitry Andric /// Schedule - Order nodes according to selected style, filling 1110b57cec5SDimitry Andric /// in the Sequence member. 1120b57cec5SDimitry Andric /// 1130b57cec5SDimitry Andric virtual void Schedule() = 0; 1140b57cec5SDimitry Andric 1150b57cec5SDimitry Andric /// VerifyScheduledSequence - Verify that all SUnits are scheduled and 1160b57cec5SDimitry Andric /// consistent with the Sequence of scheduled instructions. 1170b57cec5SDimitry Andric void VerifyScheduledSequence(bool isBottomUp); 1180b57cec5SDimitry Andric 1190b57cec5SDimitry Andric /// EmitSchedule - Insert MachineInstrs into the MachineBasicBlock 1200b57cec5SDimitry Andric /// according to the order specified in Sequence. 1210b57cec5SDimitry Andric /// 1220b57cec5SDimitry Andric virtual MachineBasicBlock* 1230b57cec5SDimitry Andric EmitSchedule(MachineBasicBlock::iterator &InsertPos); 1240b57cec5SDimitry Andric 1250b57cec5SDimitry Andric void dumpNode(const SUnit &SU) const override; 1260b57cec5SDimitry Andric void dump() const override; 1270b57cec5SDimitry Andric void dumpSchedule() const; 1280b57cec5SDimitry Andric 1290b57cec5SDimitry Andric std::string getGraphNodeLabel(const SUnit *SU) const override; 1300b57cec5SDimitry Andric 1310b57cec5SDimitry Andric std::string getDAGName() const override; 1320b57cec5SDimitry Andric 1330b57cec5SDimitry Andric virtual void getCustomGraphFeatures(GraphWriter<ScheduleDAG*> &GW) const; 1340b57cec5SDimitry Andric 1350b57cec5SDimitry Andric /// RegDefIter - In place iteration over the values defined by an 1360b57cec5SDimitry Andric /// SUnit. This does not need copies of the iterator or any other STLisms. 1370b57cec5SDimitry Andric /// The iterator creates itself, rather than being provided by the SchedDAG. 1380b57cec5SDimitry Andric class RegDefIter { 1390b57cec5SDimitry Andric const ScheduleDAGSDNodes *SchedDAG; 1400b57cec5SDimitry Andric const SDNode *Node; 1411fd87a68SDimitry Andric unsigned DefIdx = 0; 1421fd87a68SDimitry Andric unsigned NodeNumDefs = 0; 1430b57cec5SDimitry Andric MVT ValueType; 1440b57cec5SDimitry Andric 1450b57cec5SDimitry Andric public: 1460b57cec5SDimitry Andric RegDefIter(const SUnit *SU, const ScheduleDAGSDNodes *SD); 1470b57cec5SDimitry Andric IsValid()1480b57cec5SDimitry Andric bool IsValid() const { return Node != nullptr; } 1490b57cec5SDimitry Andric GetValue()1500b57cec5SDimitry Andric MVT GetValue() const { 1510b57cec5SDimitry Andric assert(IsValid() && "bad iterator"); 1520b57cec5SDimitry Andric return ValueType; 1530b57cec5SDimitry Andric } 1540b57cec5SDimitry Andric GetNode()1550b57cec5SDimitry Andric const SDNode *GetNode() const { 1560b57cec5SDimitry Andric return Node; 1570b57cec5SDimitry Andric } 1580b57cec5SDimitry Andric GetIdx()1590b57cec5SDimitry Andric unsigned GetIdx() const { 1600b57cec5SDimitry Andric return DefIdx-1; 1610b57cec5SDimitry Andric } 1620b57cec5SDimitry Andric 1630b57cec5SDimitry Andric void Advance(); 1640b57cec5SDimitry Andric 1650b57cec5SDimitry Andric private: 1660b57cec5SDimitry Andric void InitNodeNumDefs(); 1670b57cec5SDimitry Andric }; 1680b57cec5SDimitry Andric 1690b57cec5SDimitry Andric protected: 1700b57cec5SDimitry Andric /// ForceUnitLatencies - Return true if all scheduling edges should be given 1710b57cec5SDimitry Andric /// a latency value of one. The default is to return false; schedulers may 1720b57cec5SDimitry Andric /// override this as needed. forceUnitLatencies()1730b57cec5SDimitry Andric virtual bool forceUnitLatencies() const { return false; } 1740b57cec5SDimitry Andric 1750b57cec5SDimitry Andric private: 1760b57cec5SDimitry Andric /// ClusterNeighboringLoads - Cluster loads from "near" addresses into 1770b57cec5SDimitry Andric /// combined SUnits. 1780b57cec5SDimitry Andric void ClusterNeighboringLoads(SDNode *Node); 1790b57cec5SDimitry Andric /// ClusterNodes - Cluster certain nodes which should be scheduled together. 1800b57cec5SDimitry Andric /// 1810b57cec5SDimitry Andric void ClusterNodes(); 1820b57cec5SDimitry Andric 1830b57cec5SDimitry Andric /// BuildSchedUnits, AddSchedEdges - Helper functions for BuildSchedGraph. 1840b57cec5SDimitry Andric void BuildSchedUnits(); 1850b57cec5SDimitry Andric void AddSchedEdges(); 1860b57cec5SDimitry Andric 1875ffd83dbSDimitry Andric void EmitPhysRegCopy(SUnit *SU, DenseMap<SUnit*, Register> &VRBaseMap, 1880b57cec5SDimitry Andric MachineBasicBlock::iterator InsertPos); 1890b57cec5SDimitry Andric }; 1900b57cec5SDimitry Andric 1910b57cec5SDimitry Andric } // end namespace llvm 1920b57cec5SDimitry Andric 1930b57cec5SDimitry Andric #endif // LLVM_LIB_CODEGEN_SELECTIONDAG_SCHEDULEDAGSDNODES_H 194