blob: 666bdf548c718c4cdb788e92f682691c31972dfd [file] [log] [blame]
Dan Gohman6dc75fe2009-02-06 17:12:10 +00001//==- ScheduleDAGInstrs.h - MachineInstr Scheduling --------------*- C++ -*-==//
Dan Gohman343f0c02008-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 ScheduleDAGInstrs class, which implements
11// scheduling for a MachineInstr-based dependency graph.
12//
13//===----------------------------------------------------------------------===//
14
Dan Gohman6dc75fe2009-02-06 17:12:10 +000015#ifndef SCHEDULEDAGINSTRS_H
16#define SCHEDULEDAGINSTRS_H
Dan Gohman343f0c02008-11-19 23:18:57 +000017
Dan Gohman9e64bbb2009-02-10 23:27:53 +000018#include "llvm/CodeGen/MachineDominators.h"
19#include "llvm/CodeGen/MachineLoopInfo.h"
Dan Gohman343f0c02008-11-19 23:18:57 +000020#include "llvm/CodeGen/ScheduleDAG.h"
Dan Gohman9e64bbb2009-02-10 23:27:53 +000021#include "llvm/Support/Compiler.h"
Dan Gohman79ce2762009-01-15 19:20:50 +000022#include "llvm/Target/TargetRegisterInfo.h"
Evan Chengfb2e7522009-09-18 21:02:19 +000023#include "llvm/ADT/SmallSet.h"
Dan Gohman9e64bbb2009-02-10 23:27:53 +000024#include <map>
Dan Gohman343f0c02008-11-19 23:18:57 +000025
26namespace llvm {
Dan Gohman3f237442008-12-16 03:25:46 +000027 class MachineLoopInfo;
28 class MachineDominatorTree;
29
Dan Gohman9e64bbb2009-02-10 23:27:53 +000030 /// LoopDependencies - This class analyzes loop-oriented register
31 /// dependencies, which are used to guide scheduling decisions.
32 /// For example, loop induction variable increments should be
33 /// scheduled as soon as possible after the variable's last use.
34 ///
Duncan Sands16d8f8b2010-05-11 20:16:09 +000035 class LLVM_LIBRARY_VISIBILITY LoopDependencies {
Dan Gohman9e64bbb2009-02-10 23:27:53 +000036 const MachineLoopInfo &MLI;
37 const MachineDominatorTree &MDT;
38
39 public:
40 typedef std::map<unsigned, std::pair<const MachineOperand *, unsigned> >
41 LoopDeps;
42 LoopDeps Deps;
43
44 LoopDependencies(const MachineLoopInfo &mli,
45 const MachineDominatorTree &mdt) :
46 MLI(mli), MDT(mdt) {}
47
48 /// VisitLoop - Clear out any previous state and analyze the given loop.
49 ///
50 void VisitLoop(const MachineLoop *Loop) {
Andrew Tricke8deca82011-10-07 06:33:09 +000051 assert(Deps.empty() && "stale loop dependencies");
52
Dan Gohman9e64bbb2009-02-10 23:27:53 +000053 MachineBasicBlock *Header = Loop->getHeader();
54 SmallSet<unsigned, 8> LoopLiveIns;
55 for (MachineBasicBlock::livein_iterator LI = Header->livein_begin(),
56 LE = Header->livein_end(); LI != LE; ++LI)
57 LoopLiveIns.insert(*LI);
58
59 const MachineDomTreeNode *Node = MDT.getNode(Header);
60 const MachineBasicBlock *MBB = Node->getBlock();
61 assert(Loop->contains(MBB) &&
62 "Loop does not contain header!");
63 VisitRegion(Node, MBB, Loop, LoopLiveIns);
64 }
65
66 private:
67 void VisitRegion(const MachineDomTreeNode *Node,
68 const MachineBasicBlock *MBB,
69 const MachineLoop *Loop,
70 const SmallSet<unsigned, 8> &LoopLiveIns) {
71 unsigned Count = 0;
72 for (MachineBasicBlock::const_iterator I = MBB->begin(), E = MBB->end();
Jim Grosbach2f2b2542010-06-29 04:48:13 +000073 I != E; ++I) {
Dan Gohman9e64bbb2009-02-10 23:27:53 +000074 const MachineInstr *MI = I;
Jim Grosbach2f2b2542010-06-29 04:48:13 +000075 if (MI->isDebugValue())
76 continue;
Dan Gohman9e64bbb2009-02-10 23:27:53 +000077 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
78 const MachineOperand &MO = MI->getOperand(i);
79 if (!MO.isReg() || !MO.isUse())
80 continue;
81 unsigned MOReg = MO.getReg();
82 if (LoopLiveIns.count(MOReg))
83 Deps.insert(std::make_pair(MOReg, std::make_pair(&MO, Count)));
84 }
Jim Grosbach2f2b2542010-06-29 04:48:13 +000085 ++Count; // Not every iteration due to dbg_value above.
Dan Gohman9e64bbb2009-02-10 23:27:53 +000086 }
87
88 const std::vector<MachineDomTreeNode*> &Children = Node->getChildren();
89 for (std::vector<MachineDomTreeNode*>::const_iterator I =
90 Children.begin(), E = Children.end(); I != E; ++I) {
91 const MachineDomTreeNode *ChildNode = *I;
92 MachineBasicBlock *ChildBlock = ChildNode->getBlock();
93 if (Loop->contains(ChildBlock))
94 VisitRegion(ChildNode, ChildBlock, Loop, LoopLiveIns);
95 }
96 }
97 };
98
99 /// ScheduleDAGInstrs - A ScheduleDAG subclass for scheduling lists of
100 /// MachineInstrs.
Duncan Sands16d8f8b2010-05-11 20:16:09 +0000101 class LLVM_LIBRARY_VISIBILITY ScheduleDAGInstrs : public ScheduleDAG {
Dan Gohman3f237442008-12-16 03:25:46 +0000102 const MachineLoopInfo &MLI;
103 const MachineDominatorTree &MDT;
Evan Cheng38bdfc62009-10-18 19:58:47 +0000104 const MachineFrameInfo *MFI;
Evan Cheng3ef1c872010-09-10 01:29:16 +0000105 const InstrItineraryData *InstrItins;
Dan Gohman3f237442008-12-16 03:25:46 +0000106
Dan Gohman79ce2762009-01-15 19:20:50 +0000107 /// Defs, Uses - Remember where defs and uses of each physical register
108 /// are as we iterate upward through the instructions. This is allocated
109 /// here instead of inside BuildSchedGraph to avoid the need for it to be
110 /// initialized and destructed for each block.
Bob Wilsonf28dd882010-07-24 06:01:53 +0000111 std::vector<std::vector<SUnit *> > Defs;
112 std::vector<std::vector<SUnit *> > Uses;
Andrew Trick4563bba2011-10-07 06:27:02 +0000113
Dan Gohman79ce2762009-01-15 19:20:50 +0000114 /// PendingLoads - Remember where unknown loads are after the most recent
115 /// unknown store, as we iterate. As with Defs and Uses, this is here
116 /// to minimize construction/destruction.
117 std::vector<SUnit *> PendingLoads;
118
Dan Gohman9e64bbb2009-02-10 23:27:53 +0000119 /// LoopRegs - Track which registers are used for loop-carried dependencies.
120 ///
121 LoopDependencies LoopRegs;
122
123 /// LoopLiveInRegs - Track which regs are live into a loop, to help guide
124 /// back-edge-aware scheduling.
125 ///
126 SmallSet<unsigned, 8> LoopLiveInRegs;
127
Devang Patele29e8e12011-06-02 21:26:52 +0000128 protected:
129
130 /// DbgValues - Remember instruction that preceeds DBG_VALUE.
Andrew Trick4563bba2011-10-07 06:27:02 +0000131 typedef std::vector<std::pair<MachineInstr *, MachineInstr *> >
Devang Patele29e8e12011-06-02 21:26:52 +0000132 DbgValueVector;
133 DbgValueVector DbgValues;
134 MachineInstr *FirstDbgValue;
135
Dan Gohman343f0c02008-11-19 23:18:57 +0000136 public:
Dan Gohman47ac0f02009-02-11 04:27:20 +0000137 MachineBasicBlock::iterator Begin; // The beginning of the range to
138 // be scheduled. The range extends
139 // to InsertPos.
140 unsigned InsertPosIndex; // The index in BB of InsertPos.
141
Dan Gohman79ce2762009-01-15 19:20:50 +0000142 explicit ScheduleDAGInstrs(MachineFunction &mf,
143 const MachineLoopInfo &mli,
144 const MachineDominatorTree &mdt);
Dan Gohman343f0c02008-11-19 23:18:57 +0000145
146 virtual ~ScheduleDAGInstrs() {}
147
148 /// NewSUnit - Creates a new SUnit and return a ptr to it.
149 ///
150 SUnit *NewSUnit(MachineInstr *MI) {
Dan Gohman361c31d2008-12-22 21:08:08 +0000151#ifndef NDEBUG
Duncan Sandsf90fb342009-01-20 09:05:19 +0000152 const SUnit *Addr = SUnits.empty() ? 0 : &SUnits[0];
Dan Gohman361c31d2008-12-22 21:08:08 +0000153#endif
Dan Gohman343f0c02008-11-19 23:18:57 +0000154 SUnits.push_back(SUnit(MI, (unsigned)SUnits.size()));
Duncan Sandsf90fb342009-01-20 09:05:19 +0000155 assert((Addr == 0 || Addr == &SUnits[0]) &&
156 "SUnits std::vector reallocated on the fly!");
Dan Gohman343f0c02008-11-19 23:18:57 +0000157 SUnits.back().OrigNode = &SUnits.back();
158 return &SUnits.back();
159 }
160
Dan Gohman47ac0f02009-02-11 04:27:20 +0000161 /// Run - perform scheduling.
162 ///
163 void Run(MachineBasicBlock *bb,
164 MachineBasicBlock::iterator begin,
165 MachineBasicBlock::iterator end,
166 unsigned endindex);
167
Dan Gohmanc9a5b9e2008-12-23 18:36:58 +0000168 /// BuildSchedGraph - Build SUnits from the MachineBasicBlock that we are
Dan Gohman343f0c02008-11-19 23:18:57 +0000169 /// input.
Dan Gohmana70dca12009-10-09 23:27:56 +0000170 virtual void BuildSchedGraph(AliasAnalysis *AA);
Dan Gohman343f0c02008-11-19 23:18:57 +0000171
Evan Chengec6906b2010-10-23 02:10:46 +0000172 /// AddSchedBarrierDeps - Add dependencies from instructions in the current
173 /// list of instructions being scheduled to scheduling barrier. We want to
174 /// make sure instructions which define registers that are either used by
175 /// the terminator or are live-out are properly scheduled. This is
176 /// especially important when the definition latency of the return value(s)
177 /// are too high to be hidden by the branch or when the liveout registers
178 /// used by instructions in the fallthrough block.
179 void AddSchedBarrierDeps();
180
Dan Gohmanc8c28272008-11-21 00:12:10 +0000181 /// ComputeLatency - Compute node latency.
182 ///
183 virtual void ComputeLatency(SUnit *SU);
184
David Goodwindc4bdcd2009-08-19 16:08:58 +0000185 /// ComputeOperandLatency - Override dependence edge latency using
186 /// operand use/def information
187 ///
188 virtual void ComputeOperandLatency(SUnit *Def, SUnit *Use,
189 SDep& dep) const;
190
Dan Gohmanaf1d8ca2010-05-01 00:01:06 +0000191 virtual MachineBasicBlock *EmitSchedule();
Dan Gohman343f0c02008-11-19 23:18:57 +0000192
Dan Gohman9e64bbb2009-02-10 23:27:53 +0000193 /// StartBlock - Prepare to perform scheduling in the given block.
194 ///
195 virtual void StartBlock(MachineBasicBlock *BB);
196
Dan Gohman343f0c02008-11-19 23:18:57 +0000197 /// Schedule - Order nodes according to selected style, filling
198 /// in the Sequence member.
199 ///
200 virtual void Schedule() = 0;
201
Dan Gohman9e64bbb2009-02-10 23:27:53 +0000202 /// FinishBlock - Clean up after scheduling in the given block.
203 ///
204 virtual void FinishBlock();
205
Dan Gohman343f0c02008-11-19 23:18:57 +0000206 virtual void dumpNode(const SUnit *SU) const;
207
208 virtual std::string getGraphNodeLabel(const SUnit *SU) const;
209 };
210}
211
212#endif