55da5c081816d55aab89cb9b05187cdc9c41cf82
[oota-llvm.git] / lib / CodeGen / ScheduleDAGInstrs.h
1 //==- ScheduleDAGInstrs.h - MachineInstr Scheduling --------------*- C++ -*-==//
2 //
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
15 #ifndef SCHEDULEDAGINSTRS_H
16 #define SCHEDULEDAGINSTRS_H
17
18 #include "llvm/CodeGen/MachineDominators.h"
19 #include "llvm/CodeGen/MachineLoopInfo.h"
20 #include "llvm/CodeGen/ScheduleDAG.h"
21 #include "llvm/Support/Compiler.h"
22 #include "llvm/Target/TargetRegisterInfo.h"
23 #include "llvm/ADT/SmallSet.h"
24 #include <map>
25
26 namespace llvm {
27   class MachineLoopInfo;
28   class MachineDominatorTree;
29
30   /// 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 LLVM_LIBRARY_VISIBILITY 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       assert(Deps.empty() && "stale loop dependencies");
52
53       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();
73            I != E; ++I) {
74         const MachineInstr *MI = I;
75         if (MI->isDebugValue())
76           continue;
77         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         }
85         ++Count; // Not every iteration due to dbg_value above.
86       }
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.
101   class LLVM_LIBRARY_VISIBILITY ScheduleDAGInstrs : public ScheduleDAG {
102     const MachineLoopInfo &MLI;
103     const MachineDominatorTree &MDT;
104     const MachineFrameInfo *MFI;
105     const InstrItineraryData *InstrItins;
106
107     /// isPostRA flag indicates vregs cannot be present.
108     bool IsPostRA;
109
110     /// UnitLatencies flag forces single-cycle data dependencies.
111     bool UnitLatencies;
112
113     /// Defs, Uses - Remember where defs and uses of each register are as we
114     /// iterate upward through the instructions. This is allocated here instead
115     /// of inside BuildSchedGraph to avoid the need for it to be initialized and
116     /// destructed for each block.
117     std::vector<std::vector<SUnit *> > Defs;
118     std::vector<std::vector<SUnit *> > Uses;
119
120     /// PendingLoads - Remember where unknown loads are after the most recent
121     /// unknown store, as we iterate. As with Defs and Uses, this is here
122     /// to minimize construction/destruction.
123     std::vector<SUnit *> PendingLoads;
124
125     /// LoopRegs - Track which registers are used for loop-carried dependencies.
126     ///
127     LoopDependencies LoopRegs;
128
129   protected:
130
131     /// DbgValues - Remember instruction that preceeds DBG_VALUE.
132     typedef std::vector<std::pair<MachineInstr *, MachineInstr *> >
133       DbgValueVector;
134     DbgValueVector DbgValues;
135     MachineInstr *FirstDbgValue;
136
137   public:
138     MachineBasicBlock::iterator Begin;    // The beginning of the range to
139                                           // be scheduled. The range extends
140                                           // to InsertPos.
141     unsigned InsertPosIndex;              // The index in BB of InsertPos.
142
143     explicit ScheduleDAGInstrs(MachineFunction &mf,
144                                const MachineLoopInfo &mli,
145                                const MachineDominatorTree &mdt,
146                                bool IsPostRAFlag);
147
148     virtual ~ScheduleDAGInstrs() {}
149
150     /// NewSUnit - Creates a new SUnit and return a ptr to it.
151     ///
152     SUnit *NewSUnit(MachineInstr *MI) {
153 #ifndef NDEBUG
154       const SUnit *Addr = SUnits.empty() ? 0 : &SUnits[0];
155 #endif
156       SUnits.push_back(SUnit(MI, (unsigned)SUnits.size()));
157       assert((Addr == 0 || Addr == &SUnits[0]) &&
158              "SUnits std::vector reallocated on the fly!");
159       SUnits.back().OrigNode = &SUnits.back();
160       return &SUnits.back();
161     }
162
163     /// Run - perform scheduling.
164     ///
165     void Run(MachineBasicBlock *bb,
166              MachineBasicBlock::iterator begin,
167              MachineBasicBlock::iterator end,
168              unsigned endindex);
169
170     /// BuildSchedGraph - Build SUnits from the MachineBasicBlock that we are
171     /// input.
172     virtual void BuildSchedGraph(AliasAnalysis *AA);
173
174     /// AddSchedBarrierDeps - Add dependencies from instructions in the current
175     /// list of instructions being scheduled to scheduling barrier. We want to
176     /// make sure instructions which define registers that are either used by
177     /// the terminator or are live-out are properly scheduled. This is
178     /// especially important when the definition latency of the return value(s)
179     /// are too high to be hidden by the branch or when the liveout registers
180     /// used by instructions in the fallthrough block.
181     void AddSchedBarrierDeps();
182
183     /// ComputeLatency - Compute node latency.
184     ///
185     virtual void ComputeLatency(SUnit *SU);
186
187     /// ComputeOperandLatency - Override dependence edge latency using
188     /// operand use/def information
189     ///
190     virtual void ComputeOperandLatency(SUnit *Def, SUnit *Use,
191                                        SDep& dep) const;
192
193     virtual MachineBasicBlock *EmitSchedule();
194
195     /// StartBlock - Prepare to perform scheduling in the given block.
196     ///
197     virtual void StartBlock(MachineBasicBlock *BB);
198
199     /// Schedule - Order nodes according to selected style, filling
200     /// in the Sequence member.
201     ///
202     virtual void Schedule() = 0;
203
204     /// FinishBlock - Clean up after scheduling in the given block.
205     ///
206     virtual void FinishBlock();
207
208     virtual void dumpNode(const SUnit *SU) const;
209
210     virtual std::string getGraphNodeLabel(const SUnit *SU) const;
211
212   protected:
213     void addPhysRegDeps(SUnit *SU, unsigned OperIdx);
214     void addVirtRegDeps(SUnit *SU, unsigned OperIdx);
215   };
216 }
217
218 #endif