blob: 929bdaa4b178700244bb75434ae716124ad9efc0 [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/ADT/SmallSet.h"
19#include "llvm/CodeGen/MachineDominators.h"
20#include "llvm/CodeGen/MachineLoopInfo.h"
Dan Gohman343f0c02008-11-19 23:18:57 +000021#include "llvm/CodeGen/ScheduleDAG.h"
Dan Gohman9e64bbb2009-02-10 23:27:53 +000022#include "llvm/Support/Compiler.h"
Dan Gohman79ce2762009-01-15 19:20:50 +000023#include "llvm/Target/TargetRegisterInfo.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 ///
35 class VISIBILITY_HIDDEN LoopDependencies {
36 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) {
51 Deps.clear();
52 MachineBasicBlock *Header = Loop->getHeader();
53 SmallSet<unsigned, 8> LoopLiveIns;
54 for (MachineBasicBlock::livein_iterator LI = Header->livein_begin(),
55 LE = Header->livein_end(); LI != LE; ++LI)
56 LoopLiveIns.insert(*LI);
57
58 const MachineDomTreeNode *Node = MDT.getNode(Header);
59 const MachineBasicBlock *MBB = Node->getBlock();
60 assert(Loop->contains(MBB) &&
61 "Loop does not contain header!");
62 VisitRegion(Node, MBB, Loop, LoopLiveIns);
63 }
64
65 private:
66 void VisitRegion(const MachineDomTreeNode *Node,
67 const MachineBasicBlock *MBB,
68 const MachineLoop *Loop,
69 const SmallSet<unsigned, 8> &LoopLiveIns) {
70 unsigned Count = 0;
71 for (MachineBasicBlock::const_iterator I = MBB->begin(), E = MBB->end();
72 I != E; ++I, ++Count) {
73 const MachineInstr *MI = I;
74 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
75 const MachineOperand &MO = MI->getOperand(i);
76 if (!MO.isReg() || !MO.isUse())
77 continue;
78 unsigned MOReg = MO.getReg();
79 if (LoopLiveIns.count(MOReg))
80 Deps.insert(std::make_pair(MOReg, std::make_pair(&MO, Count)));
81 }
82 }
83
84 const std::vector<MachineDomTreeNode*> &Children = Node->getChildren();
85 for (std::vector<MachineDomTreeNode*>::const_iterator I =
86 Children.begin(), E = Children.end(); I != E; ++I) {
87 const MachineDomTreeNode *ChildNode = *I;
88 MachineBasicBlock *ChildBlock = ChildNode->getBlock();
89 if (Loop->contains(ChildBlock))
90 VisitRegion(ChildNode, ChildBlock, Loop, LoopLiveIns);
91 }
92 }
93 };
94
95 /// ScheduleDAGInstrs - A ScheduleDAG subclass for scheduling lists of
96 /// MachineInstrs.
97 class VISIBILITY_HIDDEN ScheduleDAGInstrs : public ScheduleDAG {
Dan Gohman3f237442008-12-16 03:25:46 +000098 const MachineLoopInfo &MLI;
99 const MachineDominatorTree &MDT;
100
Dan Gohman79ce2762009-01-15 19:20:50 +0000101 /// Defs, Uses - Remember where defs and uses of each physical register
102 /// are as we iterate upward through the instructions. This is allocated
103 /// here instead of inside BuildSchedGraph to avoid the need for it to be
104 /// initialized and destructed for each block.
105 std::vector<SUnit *> Defs[TargetRegisterInfo::FirstVirtualRegister];
106 std::vector<SUnit *> Uses[TargetRegisterInfo::FirstVirtualRegister];
107
108 /// PendingLoads - Remember where unknown loads are after the most recent
109 /// unknown store, as we iterate. As with Defs and Uses, this is here
110 /// to minimize construction/destruction.
111 std::vector<SUnit *> PendingLoads;
112
Dan Gohman9e64bbb2009-02-10 23:27:53 +0000113 /// LoopRegs - Track which registers are used for loop-carried dependencies.
114 ///
115 LoopDependencies LoopRegs;
116
117 /// LoopLiveInRegs - Track which regs are live into a loop, to help guide
118 /// back-edge-aware scheduling.
119 ///
120 SmallSet<unsigned, 8> LoopLiveInRegs;
121
Dan Gohman343f0c02008-11-19 23:18:57 +0000122 public:
Dan Gohman47ac0f02009-02-11 04:27:20 +0000123 MachineBasicBlock *BB; // Current basic block
124 MachineBasicBlock::iterator Begin; // The beginning of the range to
125 // be scheduled. The range extends
126 // to InsertPos.
127 unsigned InsertPosIndex; // The index in BB of InsertPos.
128
Dan Gohman79ce2762009-01-15 19:20:50 +0000129 explicit ScheduleDAGInstrs(MachineFunction &mf,
130 const MachineLoopInfo &mli,
131 const MachineDominatorTree &mdt);
Dan Gohman343f0c02008-11-19 23:18:57 +0000132
133 virtual ~ScheduleDAGInstrs() {}
134
135 /// NewSUnit - Creates a new SUnit and return a ptr to it.
136 ///
137 SUnit *NewSUnit(MachineInstr *MI) {
Dan Gohman361c31d2008-12-22 21:08:08 +0000138#ifndef NDEBUG
Duncan Sandsf90fb342009-01-20 09:05:19 +0000139 const SUnit *Addr = SUnits.empty() ? 0 : &SUnits[0];
Dan Gohman361c31d2008-12-22 21:08:08 +0000140#endif
Dan Gohman343f0c02008-11-19 23:18:57 +0000141 SUnits.push_back(SUnit(MI, (unsigned)SUnits.size()));
Duncan Sandsf90fb342009-01-20 09:05:19 +0000142 assert((Addr == 0 || Addr == &SUnits[0]) &&
143 "SUnits std::vector reallocated on the fly!");
Dan Gohman343f0c02008-11-19 23:18:57 +0000144 SUnits.back().OrigNode = &SUnits.back();
145 return &SUnits.back();
146 }
147
Dan Gohman47ac0f02009-02-11 04:27:20 +0000148 /// Run - perform scheduling.
149 ///
150 void Run(MachineBasicBlock *bb,
151 MachineBasicBlock::iterator begin,
152 MachineBasicBlock::iterator end,
153 unsigned endindex);
154
Dan Gohmanc9a5b9e2008-12-23 18:36:58 +0000155 /// BuildSchedGraph - Build SUnits from the MachineBasicBlock that we are
Dan Gohman343f0c02008-11-19 23:18:57 +0000156 /// input.
Dan Gohmanc9a5b9e2008-12-23 18:36:58 +0000157 virtual void BuildSchedGraph();
Dan Gohman343f0c02008-11-19 23:18:57 +0000158
Dan Gohmanc8c28272008-11-21 00:12:10 +0000159 /// ComputeLatency - Compute node latency.
160 ///
161 virtual void ComputeLatency(SUnit *SU);
162
David Goodwindc4bdcd2009-08-19 16:08:58 +0000163 /// ComputeOperandLatency - Override dependence edge latency using
164 /// operand use/def information
165 ///
166 virtual void ComputeOperandLatency(SUnit *Def, SUnit *Use,
167 SDep& dep) const;
168
Dan Gohman343f0c02008-11-19 23:18:57 +0000169 virtual MachineBasicBlock *EmitSchedule();
170
Dan Gohman9e64bbb2009-02-10 23:27:53 +0000171 /// StartBlock - Prepare to perform scheduling in the given block.
172 ///
173 virtual void StartBlock(MachineBasicBlock *BB);
174
Dan Gohman343f0c02008-11-19 23:18:57 +0000175 /// Schedule - Order nodes according to selected style, filling
176 /// in the Sequence member.
177 ///
178 virtual void Schedule() = 0;
179
Dan Gohman9e64bbb2009-02-10 23:27:53 +0000180 /// FinishBlock - Clean up after scheduling in the given block.
181 ///
182 virtual void FinishBlock();
183
Dan Gohman343f0c02008-11-19 23:18:57 +0000184 virtual void dumpNode(const SUnit *SU) const;
185
186 virtual std::string getGraphNodeLabel(const SUnit *SU) const;
187 };
188}
189
190#endif