blob: a058942c5689cd58cdd386e33fafb9611486730c [file] [log] [blame]
Dan Gohman483377c2009-02-06 17:22:58 +00001//===---- ScheduleDAGSDNodes.h - SDNode Scheduling --------------*- C++ -*-===//
Dan Gohman60cb69e2008-11-19 23:18:57 +00002//
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// This file implements the ScheduleDAGSDNodes class, which implements
11// scheduling for an SDNode-based dependency graph.
12//
13//===----------------------------------------------------------------------===//
14
Benjamin Kramera7c40ef2014-08-13 16:26:38 +000015#ifndef LLVM_LIB_CODEGEN_SELECTIONDAG_SCHEDULEDAGSDNODES_H
16#define LLVM_LIB_CODEGEN_SELECTIONDAG_SCHEDULEDAGSDNODES_H
Dan Gohman60cb69e2008-11-19 23:18:57 +000017
Eugene Zelenkoc4ad1ce2017-01-11 01:45:03 +000018#include "llvm/CodeGen/ISDOpcodes.h"
Jakub Staszak8bc7af12013-02-20 00:26:25 +000019#include "llvm/CodeGen/MachineBasicBlock.h"
Eugene Zelenkoc4ad1ce2017-01-11 01:45:03 +000020#include "llvm/CodeGen/MachineValueType.h"
Dan Gohman60cb69e2008-11-19 23:18:57 +000021#include "llvm/CodeGen/ScheduleDAG.h"
Eugene Zelenkoc4ad1ce2017-01-11 01:45:03 +000022#include "llvm/CodeGen/SelectionDAGNodes.h"
23#include "llvm/Support/Casting.h"
24#include <cassert>
25#include <string>
26#include <vector>
Dan Gohman60cb69e2008-11-19 23:18:57 +000027
28namespace llvm {
Eugene Zelenkoc4ad1ce2017-01-11 01:45:03 +000029
30class InstrItineraryData;
31
Dan Gohman7782de72008-12-22 21:06:20 +000032 /// ScheduleDAGSDNodes - A ScheduleDAG for scheduling SDNode-based DAGs.
Andrew Trickd0548ae2011-02-04 03:18:17 +000033 ///
Dan Gohman7782de72008-12-22 21:06:20 +000034 /// Edges between SUnits are initially based on edges in the SelectionDAG,
35 /// and additional edges can be added by the schedulers as heuristics.
36 /// SDNodes such as Constants, Registers, and a few others that are not
37 /// interesting to schedulers are not allocated SUnits.
38 ///
Chris Lattner3e5fbd72010-12-21 02:38:05 +000039 /// SDNodes with MVT::Glue operands are grouped along with the flagged
Dan Gohman7782de72008-12-22 21:06:20 +000040 /// nodes into a single SUnit so that they are scheduled together.
41 ///
42 /// SDNode-based scheduling graphs do not use SDep::Anti or SDep::Output
43 /// edges. Physical register dependence information is not carried in
44 /// the DAG and must be handled explicitly by schedulers.
45 ///
Benjamin Kramer079b96e2013-09-11 18:05:11 +000046 class ScheduleDAGSDNodes : public ScheduleDAG {
Dan Gohman60cb69e2008-11-19 23:18:57 +000047 public:
Andrew Trick60cf03e2012-03-07 05:21:52 +000048 MachineBasicBlock *BB;
Dan Gohmandfaf6462009-02-11 04:27:20 +000049 SelectionDAG *DAG; // DAG of the current basic block
Evan Chengbf407072010-09-10 01:29:16 +000050 const InstrItineraryData *InstrItins;
Dan Gohmandfaf6462009-02-11 04:27:20 +000051
Andrew Trick60cf03e2012-03-07 05:21:52 +000052 /// The schedule. Null SUnit*'s represent noop instructions.
53 std::vector<SUnit*> Sequence;
54
Dan Gohman619ef482009-01-15 19:20:50 +000055 explicit ScheduleDAGSDNodes(MachineFunction &mf);
Dan Gohman60cb69e2008-11-19 23:18:57 +000056
Eugene Zelenkoc4ad1ce2017-01-11 01:45:03 +000057 ~ScheduleDAGSDNodes() override = default;
Dan Gohman60cb69e2008-11-19 23:18:57 +000058
Dan Gohmandfaf6462009-02-11 04:27:20 +000059 /// Run - perform scheduling.
60 ///
Andrew Trick60cf03e2012-03-07 05:21:52 +000061 void Run(SelectionDAG *dag, MachineBasicBlock *bb);
Dan Gohmandfaf6462009-02-11 04:27:20 +000062
Dan Gohman60cb69e2008-11-19 23:18:57 +000063 /// isPassiveNode - Return true if the node is a non-scheduled leaf.
64 ///
65 static bool isPassiveNode(SDNode *Node) {
66 if (isa<ConstantSDNode>(Node)) return true;
67 if (isa<ConstantFPSDNode>(Node)) return true;
68 if (isa<RegisterSDNode>(Node)) return true;
Jakob Stoklund Olesen9349351d2012-01-18 23:52:12 +000069 if (isa<RegisterMaskSDNode>(Node)) return true;
Dan Gohman60cb69e2008-11-19 23:18:57 +000070 if (isa<GlobalAddressSDNode>(Node)) return true;
71 if (isa<BasicBlockSDNode>(Node)) return true;
72 if (isa<FrameIndexSDNode>(Node)) return true;
73 if (isa<ConstantPoolSDNode>(Node)) return true;
Jakob Stoklund Olesen505715d2012-08-07 22:37:05 +000074 if (isa<TargetIndexSDNode>(Node)) return true;
Dan Gohman60cb69e2008-11-19 23:18:57 +000075 if (isa<JumpTableSDNode>(Node)) return true;
76 if (isa<ExternalSymbolSDNode>(Node)) return true;
Rafael Espindola36b718f2015-06-22 17:46:53 +000077 if (isa<MCSymbolSDNode>(Node)) return true;
Dan Gohman6c938802009-10-30 01:27:03 +000078 if (isa<BlockAddressSDNode>(Node)) return true;
Chris Lattner3b9f02a2010-04-07 05:20:54 +000079 if (Node->getOpcode() == ISD::EntryToken ||
80 isa<MDNodeSDNode>(Node)) return true;
Dan Gohman60cb69e2008-11-19 23:18:57 +000081 return false;
82 }
83
84 /// NewSUnit - Creates a new SUnit and return a ptr to it.
85 ///
Andrew Trick52226d42012-03-07 23:00:49 +000086 SUnit *newSUnit(SDNode *N);
Dan Gohman60cb69e2008-11-19 23:18:57 +000087
88 /// Clone - Creates a clone of the specified SUnit. It does not copy the
89 /// predecessors / successors info nor the temporary scheduling states.
90 ///
91 SUnit *Clone(SUnit *N);
Andrew Trickd0548ae2011-02-04 03:18:17 +000092
Dan Gohman04543e72008-12-23 18:36:58 +000093 /// BuildSchedGraph - Build the SUnit graph from the selection dag that we
94 /// are input. This SUnit graph is similar to the SelectionDAG, but
95 /// excludes nodes that aren't interesting to scheduling, and represents
96 /// flagged together nodes with a single SUnit.
Andrew Trick0c84efe2012-03-07 00:18:12 +000097 void BuildSchedGraph(AliasAnalysis *AA);
Dan Gohman60cb69e2008-11-19 23:18:57 +000098
Andrew Trickd0548ae2011-02-04 03:18:17 +000099 /// InitNumRegDefsLeft - Determine the # of regs defined by this node.
100 ///
101 void InitNumRegDefsLeft(SUnit *SU);
102
Andrew Trick52226d42012-03-07 23:00:49 +0000103 /// computeLatency - Compute node latency.
Dan Gohman60cb69e2008-11-19 23:18:57 +0000104 ///
Andrew Trick52226d42012-03-07 23:00:49 +0000105 virtual void computeLatency(SUnit *SU);
Dan Gohman60cb69e2008-11-19 23:18:57 +0000106
Andrew Trick52226d42012-03-07 23:00:49 +0000107 virtual void computeOperandLatency(SDNode *Def, SDNode *Use,
Evan Chengbdd062d2010-05-20 06:13:19 +0000108 unsigned OpIdx, SDep& dep) const;
109
Dan Gohman60cb69e2008-11-19 23:18:57 +0000110 /// Schedule - Order nodes according to selected style, filling
111 /// in the Sequence member.
112 ///
113 virtual void Schedule() = 0;
114
Andrew Trick46a58662012-03-07 05:21:36 +0000115 /// VerifyScheduledSequence - Verify that all SUnits are scheduled and
116 /// consistent with the Sequence of scheduled instructions.
117 void VerifyScheduledSequence(bool isBottomUp);
118
Andrew Tricke932bb72012-03-07 05:21:44 +0000119 /// EmitSchedule - Insert MachineInstrs into the MachineBasicBlock
120 /// according to the order specified in Sequence.
121 ///
Evan Cheng839fb652012-10-17 19:39:36 +0000122 virtual MachineBasicBlock*
123 EmitSchedule(MachineBasicBlock::iterator &InsertPos);
Andrew Tricke932bb72012-03-07 05:21:44 +0000124
Craig Topper7b883b32014-03-08 06:31:39 +0000125 void dumpNode(const SUnit *SU) const override;
Dan Gohman60cb69e2008-11-19 23:18:57 +0000126
Andrew Trickedee68c2012-03-07 05:21:40 +0000127 void dumpSchedule() const;
128
Craig Topper7b883b32014-03-08 06:31:39 +0000129 std::string getGraphNodeLabel(const SUnit *SU) const override;
Dan Gohman60cb69e2008-11-19 23:18:57 +0000130
Craig Topper7b883b32014-03-08 06:31:39 +0000131 std::string getDAGName() const override;
Andrew Trick1b2324d2012-03-07 00:18:22 +0000132
Dan Gohman60cb69e2008-11-19 23:18:57 +0000133 virtual void getCustomGraphFeatures(GraphWriter<ScheduleDAG*> &GW) const;
134
Andrew Trickd0548ae2011-02-04 03:18:17 +0000135 /// RegDefIter - In place iteration over the values defined by an
136 /// SUnit. This does not need copies of the iterator or any other STLisms.
137 /// The iterator creates itself, rather than being provided by the SchedDAG.
138 class RegDefIter {
139 const ScheduleDAGSDNodes *SchedDAG;
140 const SDNode *Node;
141 unsigned DefIdx;
142 unsigned NodeNumDefs;
Patrik Hagglund05394352012-12-13 18:45:35 +0000143 MVT ValueType;
Eugene Zelenkoc4ad1ce2017-01-11 01:45:03 +0000144
Andrew Trickd0548ae2011-02-04 03:18:17 +0000145 public:
146 RegDefIter(const SUnit *SU, const ScheduleDAGSDNodes *SD);
147
Craig Topperada08572014-04-16 04:21:27 +0000148 bool IsValid() const { return Node != nullptr; }
Andrew Trickd0548ae2011-02-04 03:18:17 +0000149
Patrik Hagglund05394352012-12-13 18:45:35 +0000150 MVT GetValue() const {
Andrew Trickd0548ae2011-02-04 03:18:17 +0000151 assert(IsValid() && "bad iterator");
152 return ValueType;
153 }
154
Owen Anderson96adc4a2011-06-15 23:35:18 +0000155 const SDNode *GetNode() const {
156 return Node;
157 }
158
159 unsigned GetIdx() const {
Owen Andersonb0a5a1e2011-06-27 18:34:12 +0000160 return DefIdx-1;
Owen Anderson96adc4a2011-06-15 23:35:18 +0000161 }
162
Andrew Trickd0548ae2011-02-04 03:18:17 +0000163 void Advance();
Eugene Zelenkoc4ad1ce2017-01-11 01:45:03 +0000164
Andrew Trickd0548ae2011-02-04 03:18:17 +0000165 private:
166 void InitNodeNumDefs();
167 };
168
Andrew Trick09650df2012-10-08 18:53:57 +0000169 protected:
170 /// ForceUnitLatencies - Return true if all scheduling edges should be given
171 /// a latency value of one. The default is to return false; schedulers may
172 /// override this as needed.
173 virtual bool forceUnitLatencies() const { return false; }
174
Dan Gohman60cb69e2008-11-19 23:18:57 +0000175 private:
Evan Cheng9d92aaa2010-01-22 03:36:51 +0000176 /// ClusterNeighboringLoads - Cluster loads from "near" addresses into
177 /// combined SUnits.
Evan Cheng38f65602010-06-10 02:09:31 +0000178 void ClusterNeighboringLoads(SDNode *Node);
179 /// ClusterNodes - Cluster certain nodes which should be scheduled together.
180 ///
181 void ClusterNodes();
Evan Cheng9d92aaa2010-01-22 03:36:51 +0000182
Dan Gohman04543e72008-12-23 18:36:58 +0000183 /// BuildSchedUnits, AddSchedEdges - Helper functions for BuildSchedGraph.
184 void BuildSchedUnits();
185 void AddSchedEdges();
Andrew Tricke932bb72012-03-07 05:21:44 +0000186
187 void EmitPhysRegCopy(SUnit *SU, DenseMap<SUnit*, unsigned> &VRBaseMap,
188 MachineBasicBlock::iterator InsertPos);
Dan Gohman60cb69e2008-11-19 23:18:57 +0000189 };
Dan Gohman60cb69e2008-11-19 23:18:57 +0000190
Eugene Zelenkoc4ad1ce2017-01-11 01:45:03 +0000191} // end namespace llvm
192
193#endif // LLVM_LIB_CODEGEN_SELECTIONDAG_SCHEDULEDAGSDNODES_H